June 27, 2007 Storing a list in an expression list | ||||
---|---|---|---|---|
| ||||
Reposting on the correct group. Sorry. Hi, I'm starting with D and I want to use it for a project on skool. I want to write a new scriptiing language but I dont' know how to store a list on my expression list. I have this structure: class Node(T) { public: T value Node!(T) left; Node!(T) right; // Constructors... } And I use it like this: // a = b + c; Node!(string) expression = new Node!(string)("=", new Node!(string)("a", new Node!(string)("=", new Node!(string)("b", new Node!(string)("c"))); Which gives me smth like: = a + b c But i dont' know how to store a list of parameters: func(1, 2, 3); expression = new Node!(string)("(", new Node!(string)("func"), .... ??? ); What do you suggest? tnks |
June 27, 2007 Re: Storing a list in an expression list | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lurker #5 | Lurker #5 <lnumber5@klassmaster.com> schrieb (Tue, 26 Jun 2007 22:13:00 -0400): > Reposting on the correct group. Sorry. > > Hi, > > I'm starting with D and I want to use it for a project on skool. I want to write a new scriptiing language but I dont' know how to store a list on my expression list. When you are dealing with expressions I think you basically have two choices: Store them as a tree which reflects their structure or store them as a list with some stack-model in mind. In the latter case you have to know the exact number of sub-expressions for each expression. Your stuff as tree in pseudo-d: class TreeNode { ... } class Assignment : TreeNode { ... } class Operation : TreeNode { ... } class Variable : TreeNode { ... } class LVlaue : TreeNode { ... } // ... // a = b + c --> =(a, +(b, c)) abc = Assignment( LValue("a"), Operation("+", Variable("b"), Variable("c") ) ) call = FunctionCall("func", Literal(1), Literal(2), Literal(3) ) Note that this requires transformings like a = b + c --> =(a, +(b, c)) to be done elsewhere. I did never write a language myself (there are already more than enough out there) but I think the tree thingy is the most common way. Note that it is also easily convertable to/from an EBNF or somthing equivalent. Every context-free grammer can be parsed using a stack, I guess this is also common, maybe both methods are even used in conjunction but someone else in this NG should know much more about this. Also Im not sure if I using the stack in the "correct" way here so hello everyone, please correct me if thats bullshit. Assume you have enchanched your list so you have a real "List" class which has a 'push' (=append) method and a pop method that removes and returns the last element (for easier writing and understanding) interpret(inout stack, expr, n = 1) { repeat n times { e = expr.getElement() if(e.isInfix) { // Now reoder the shit so things like "x = y" transform to // a stack like [x, y, =] which is easy to "read" afterwards // The left expr is already on the right place interpret(expr[1:]) // Push right expr stack.push(e) } else if(e.subExprs > 0) { // This is a simplified function call thingy // imagine the function call to be written as // "func 1 2 3". we put 1 2 3 on the stack, then "func" interpret(expr[1:], e.subExprs) // Push right-side stuffies stack.push(e) } else { // e.subExprs == 0 stack.push(e) } } // repeat } I hope that does more help then confuse. As suggested, dont take it too serious in the details but maybe it gives you some hint which direction to think in. Note also that the web should be full of sourcecode of parsers for existing and non existing languages. If the task explicitely limits you to use Lists, then I'd guess they want something stack-alike. HTH Henning -- GPG Public Key: http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851 Fingerprint: 344F 4072 F038 BB9E B35D E6AB DDD6 D36D 4191 1851 |
Copyright © 1999-2021 by the D Language Foundation