Loading ...
Sorry, an error occurred while loading the content.
 

mixed value hashtable

Expand Messages
  • mo.deeq
    Hi all, hope all is well I wondered if anyone had an idea of how to get around needing a hashtable in which all the keys are strings, but, the values may be of
    Message 1 of 12 , Jun 5, 2007
      Hi all, hope all is well
      I wondered if anyone had an idea of how to get around needing a
      hashtable in which all the keys are strings, but, the values may be of
      any type. In effect some thing along the lines of:

      val mutable table : (string, 'a) ExtLib.Hashtbl.t


      the reason being that i'm writing an interpreter were variables are
      defined and assigned as in the language, i.e.

      int a = 1
      char b = "b"
      ...

      and so on. I would like to therefore keep a table of the values held
      by various variables while my interpreter does its job.


      thank you in advance,
      deeq.
    • mo.deeq
      ... Sorry, forgot to mention, so far i m using an object called assignedVar, defined as: class assignedVar ( v_typ:string) (name:string ) (val:string) where
      Message 2 of 12 , Jun 5, 2007
        --- In ocaml_beginners@yahoogroups.com, "mo.deeq" <mo.deeq@...> wrote:
        >
        > Hi all, hope all is well
        > I wondered if anyone had an idea of how to get around needing a
        > hashtable in which all the keys are strings, but, the values may be of
        > any type. In effect some thing along the lines of:
        >
        > val mutable table : (string, 'a) ExtLib.Hashtbl.t
        >
        >
        > the reason being that i'm writing an interpreter were variables are
        > defined and assigned as in the language, i.e.
        >
        > int a = 1
        > char b = "b"
        > ...
        >
        > and so on. I would like to therefore keep a table of the values held
        > by various variables while my interpreter does its job.
        >
        >
        > thank you in advance,
        > deeq.
        >


        Sorry, forgot to mention, so far i'm using an object called
        assignedVar, defined as:

        class assignedVar ( v_typ:string) (name:string ) (val:string)

        where i'm keeping everything as a string and casting based on the the
        type as specified by 'v_typ'..

        I was hoping for a simpler and more efficient mechanism.

        thanks
        -
        deeq
      • Robert Roessler
        ... If your needs are that simple (your v_typ values are known in advance), how about something *like* (your cases would be different) type prop_tbl = (string,
        Message 3 of 12 , Jun 5, 2007
          mo.deeq wrote:
          > > I wondered if anyone had an idea of how to get around needing a
          > > hashtable in which all the keys are strings, but, the values may be of
          > > any type. In effect some thing along the lines of:
          > >
          > > val mutable table : (string, 'a) ExtLib.Hashtbl.t
          > >
          > >
          > > the reason being that i'm writing an interpreter were variables are
          > > defined and assigned as in the language, i.e.
          > >
          > > int a = 1
          > > char b = "b"
          > > ...
          > >
          > > and so on. I would like to therefore keep a table of the values held
          > > by various variables while my interpreter does its job.
          >
          > Sorry, forgot to mention, so far i'm using an object called
          > assignedVar, defined as:
          >
          > class assignedVar ( v_typ:string) (name:string ) (val:string)
          >
          > where i'm keeping everything as a string and casting based on the the
          > type as specified by 'v_typ'..
          >
          > I was hoping for a simpler and more efficient mechanism.

          If your needs are that simple (your v_typ values are known in
          advance), how about something *like* (your cases would be different)

          type prop_tbl = (string, [`Str of string | `Fun of unit -> string])
          Hashtbl.t

          which creates a "properties" table whose values can be either strings
          or functions returning strings, which can then be used by [e.g.]

          let (prop_tab: prop_tbl) = Hashtbl.create 53

          Then just do a match when you pull these out...

          Robert Roessler
          robertr@...
          http://www.rftp.com
        • mo.deeq
          ... may be of ... held ... ingenious method Robert, but sadly i allow users to define their own types ..however, i m sure i will find that definition somewhere
          Message 4 of 12 , Jun 5, 2007
            --- In ocaml_beginners@yahoogroups.com, Robert Roessler <robertr@...>
            wrote:
            >
            > mo.deeq wrote:
            > > > I wondered if anyone had an idea of how to get around needing a
            > > > hashtable in which all the keys are strings, but, the values
            may be of
            > > > any type. In effect some thing along the lines of:
            > > >
            > > > val mutable table : (string, 'a) ExtLib.Hashtbl.t
            > > >
            > > >
            > > > the reason being that i'm writing an interpreter were variables are
            > > > defined and assigned as in the language, i.e.
            > > >
            > > > int a = 1
            > > > char b = "b"
            > > > ...
            > > >
            > > > and so on. I would like to therefore keep a table of the values
            held
            > > > by various variables while my interpreter does its job.
            > >
            > > Sorry, forgot to mention, so far i'm using an object called
            > > assignedVar, defined as:
            > >
            > > class assignedVar ( v_typ:string) (name:string ) (val:string)
            > >
            > > where i'm keeping everything as a string and casting based on the the
            > > type as specified by 'v_typ'..
            > >
            > > I was hoping for a simpler and more efficient mechanism.
            >
            > If your needs are that simple (your v_typ values are known in
            > advance), how about something *like* (your cases would be different)
            >
            > type prop_tbl = (string, [`Str of string | `Fun of unit -> string])
            > Hashtbl.t
            >
            > which creates a "properties" table whose values can be either strings
            > or functions returning strings, which can then be used by [e.g.]
            >
            > let (prop_tab: prop_tbl) = Hashtbl.create 53
            >
            > Then just do a match when you pull these out...
            >
            > Robert Roessler
            > robertr@...
            > http://www.rftp.com
            >

            ingenious method Robert, but sadly i allow users to define their own
            types ..however, i'm sure i will find that definition somewhere :)

            deeq
          • Karl Zilles
            ... Perhaps you should give some examples of user defined types and we can show you how to apply Robert s method to these types. You re currently storing
            Message 5 of 12 , Jun 5, 2007
              mo.deeq wrote:
              > ingenious method Robert, but sadly i allow users to define their own
              > types ..however, i'm sure i will find that definition somewhere :)

              Perhaps you should give some examples of user defined types and we can
              show you how to apply Robert's method to these types.

              You're currently storing variables as strings. If you can store them as
              strings, then we can create a variant type for you that will be cleaner
              and more efficient than your current system.
            • mo.deeq
              ... them as ... Hi Karl, your probably right i m still caml-naive, anyhow, definitions are fairly simple have the form my_type my_var = my_value i.e. int a =
              Message 6 of 12 , Jun 5, 2007
                --- In ocaml_beginners@yahoogroups.com, Karl Zilles <zilles@...> wrote:
                >
                > mo.deeq wrote:
                > > ingenious method Robert, but sadly i allow users to define their own
                > > types ..however, i'm sure i will find that definition somewhere :)
                >
                > Perhaps you should give some examples of user defined types and we can
                > show you how to apply Robert's method to these types.
                >
                > You're currently storing variables as strings. If you can store
                them as
                > strings, then we can create a variant type for you that will be cleaner
                > and more efficient than your current system.
                >

                Hi Karl, your probably right i'm still caml-naive, anyhow,
                definitions are fairly simple have the form

                my_type my_var = my_value

                i.e.

                int a = 1 ;
                char v = "v";
                foo bar = foobar

                where the only 1 constraint that object on the left and right hand
                side of the assignment *must* be of the same type and as such the type
                of a variable must be know before it can be used in an assignment, so
                that a user is able to write code such as

                foo foobar = ..;

                foo bar = foobar;


                the types and assignments are defined by the user of the language as
                and when required in their program, i don't enforce any specific
                constraints.

                i hope that makes more sense. thanks again

                deeq
              • Karl Zilles
                ... It looks like you re writing an interpreter for a language. Is this a well know language, or one you re creating? If it s one you re creating, what is the
                Message 7 of 12 , Jun 5, 2007
                  mo.deeq wrote:
                  > definitions are fairly simple have the form
                  >
                  > my_type my_var = my_value
                  >
                  > i.e.
                  >
                  > int a = 1 ;
                  > char v = "v";
                  > foo bar = foobar
                  >
                  > where the only 1 constraint that object on the left and right hand
                  > side of the assignment *must* be of the same type and as such the type
                  > of a variable must be know before it can be used in an assignment, so
                  > that a user is able to write code such as
                  >
                  > foo foobar = ..;
                  >
                  > foo bar = foobar;
                  >
                  >
                  > the types and assignments are defined by the user of the language as
                  > and when required in their program, i don't enforce any specific
                  > constraints.

                  It looks like you're writing an interpreter for a language. Is this a
                  well know language, or one you're creating?

                  If it's one you're creating, what is the syntax for defining a type?

                  Here's what I'm getting at... Let's say that your variable storage type
                  (in ocaml) is:

                  type variable = String of string | Int of int | Block of variable array;;

                  Then if your user defined a "person" record type in your language with
                  two fields, say an string called "name" and a integer called "age", you
                  could use the "variable" type to store the person like so:

                  Block [| String "Hello"; Int 47 |];;

                  (Your interpreter would remember that "name" is always the first index
                  into a "person" and age always the second.)
                • Karl Zilles
                  ... For clarity.
                  Message 8 of 12 , Jun 5, 2007
                    Karl Zilles wrote:
                    > Block [| String "Hello"; Int 47 |];;

                    Make that:

                    > Block [| String "Karl"; Int 36 |];;

                    For clarity.
                  • mo.deeq
                    ... array;; ... Hi Karl, yes its an interpreter for my own language. I havent defined any thing as complex as record, though i would like to, the parser gives
                    Message 9 of 12 , Jun 5, 2007
                      --- In ocaml_beginners@yahoogroups.com, Karl Zilles <zilles@...> wrote:
                      >
                      > mo.deeq wrote:
                      > > definitions are fairly simple have the form
                      > >
                      > > my_type my_var = my_value
                      > >
                      > > i.e.
                      > >
                      > > int a = 1 ;
                      > > char v = "v";
                      > > foo bar = foobar
                      > >
                      > > where the only 1 constraint that object on the left and right hand
                      > > side of the assignment *must* be of the same type and as such the type
                      > > of a variable must be know before it can be used in an assignment, so
                      > > that a user is able to write code such as
                      > >
                      > > foo foobar = ..;
                      > >
                      > > foo bar = foobar;
                      > >
                      > >
                      > > the types and assignments are defined by the user of the language as
                      > > and when required in their program, i don't enforce any specific
                      > > constraints.
                      >
                      > It looks like you're writing an interpreter for a language. Is this a
                      > well know language, or one you're creating?
                      >
                      > If it's one you're creating, what is the syntax for defining a type?
                      >
                      > Here's what I'm getting at... Let's say that your variable storage type
                      > (in ocaml) is:
                      >
                      > type variable = String of string | Int of int | Block of variable
                      array;;
                      >
                      > Then if your user defined a "person" record type in your language with
                      > two fields, say an string called "name" and a integer called "age", you
                      > could use the "variable" type to store the person like so:
                      >
                      > Block [| String "Hello"; Int 47 |];;
                      >
                      > (Your interpreter would remember that "name" is always the first index
                      > into a "person" and age always the second.)
                      >


                      Hi Karl, yes its an interpreter for my own language. I havent defined
                      any thing as complex as record, though i would like to, the parser
                      gives the following;


                      declaration_list
                      : declaration{[$1]}
                      | declaration_list declaration{$1@[$2]}
                      ;

                      declarator_list
                      : declarator {[$1]}
                      | declarator_list COMMA declarator{$1@[$3]}
                      ;

                      declaration
                      : type_specifier SEMICOLON
                      {Declaration($1, None, !Lexer.line_counter)}
                      | type_specifier declarator_list SEMICOLON
                      {Declaration($1, Some(DeclaratorList($2)), !Lexer.line_counter)}
                      ;

                      declarator
                      : identifier
                      {Declarator($1)}
                      | PARENTHESIS_OPEN declarator PARENTHESIS_CLOSE
                      {Declarator($2)}
                      | declarator BRACKET_OPEN constant_expression BRACKET_CLOSE
                      {DeclaratorArray($1, $3)}
                      ;

                      type_specifier
                      : IDENT {Type(TypeSingle ($1))}
                      ;


                      This is used to build the AST. where in the AST, a type is very dump
                      and defined as:

                      type basic_type =
                      TypeSingle of string
                      | TypeComposite of string * string
                      | None


                      and the declarations and declarators are:

                      type ast =
                      ...
                      | DeclarationList of ast list
                      | Declaration of ast * ast option * int
                      | Declarator of ast
                      | DeclaratorList of ast list
                      | DeclaratorArray of ast * ast



                      so for example, given

                      int a, b;

                      foo bar;
                      foo foobar ;
                      bar = foobar;


                      we produce the following AST


                      DeclarationList(
                      Declaration(Type(TypeInt),
                      DeclaratorList(
                      Declarator(Identifier(a)),
                      Declarator(Identifier(b))
                      )
                      ),
                      Declaration(Type(foo),
                      DeclaratorList(
                      Declarator(Identifier(bar))
                      )
                      ),
                      Declaration(Type(foo),
                      DeclaratorList(
                      Declarator(Identifier(foobar))
                      )
                      )
                      ),

                      and the assignment ends up being represented as:

                      ExpressionStatement(Expression(
                      AssignmentExpression(
                      Identifier(bar),
                      Assignment,
                      Identifier(foobar)
                      ))),


                      thanks
                      -- deeq
                    • Karl Zilles
                      ... But what is a foo? Where is a foo defined?
                      Message 10 of 12 , Jun 5, 2007
                        mo.deeq wrote:
                        > Karl Zilles <zilles@...> wrote:
                        >> what is the syntax for defining a type?
                        >>
                        > so for example, given
                        >
                        > int a, b;
                        >
                        > foo bar;
                        > foo foobar ;
                        > bar = foobar;

                        But what is a foo? Where is a foo defined?
                      • mo.deeq
                        ... hi karl, currently, for simplicity (and due to my caml pace) my parser is ignoring the fact it needs the record foo to be defined. and is treating it
                        Message 11 of 12 , Jun 5, 2007
                          --- In ocaml_beginners@yahoogroups.com, Karl Zilles <zilles@...> wrote:
                          >
                          > mo.deeq wrote:
                          > > Karl Zilles <zilles@> wrote:
                          > >> what is the syntax for defining a type?
                          > >>
                          > > so for example, given
                          > >
                          > > int a, b;
                          > >
                          > > foo bar;
                          > > foo foobar ;
                          > > bar = foobar;
                          >
                          > But what is a foo? Where is a foo defined?
                          >

                          hi karl,
                          currently, for simplicity (and due to my caml pace) my parser is
                          ignoring the fact it needs the record 'foo' to be defined. and is
                          treating it as a string. i wanted to be able to evaluate simple
                          expression before apply it. My skeleton place holder defines records as

                          <record> Identifier {
                          DeclarationList
                          }

                          i.e
                          record foo{
                          int a; int b; ....
                          }

                          with much the same aim as the person example you listed. records may
                          also contain sub-records and I belief that i should be able to expand
                          every thing to a strings we i do substitutions in the interpreter.

                          --
                          deeq
                        • Richard Jones
                          ... So what should happen if you did: Hashtbl.add table key1 string ;; let an_int = (Hashtbl.get table key1 : int) ;; (In other words, put in a string,
                          Message 12 of 12 , Jun 6, 2007
                            On Tue, Jun 05, 2007 at 08:47:50PM -0000, mo.deeq wrote:
                            > Hi all, hope all is well
                            > I wondered if anyone had an idea of how to get around needing a
                            > hashtable in which all the keys are strings, but, the values may be of
                            > any type. In effect some thing along the lines of:
                            >
                            > val mutable table : (string, 'a) ExtLib.Hashtbl.t

                            So what should happen if you did:

                            Hashtbl.add table "key1" "string" ;;
                            let an_int = (Hashtbl.get table "key1" : int) ;;

                            (In other words, put in a string, but try to get it out as an int)?

                            This is what static typing is meant to stop you from doing.

                            Rich.

                            --
                            Richard Jones
                            Red Hat
                          Your message has been successfully submitted and would be delivered to recipients shortly.