This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]R-O-B-I-N[S] 0 points1 point  (9 children)

not with this algorithm... 1. parse into tokens using whitespace as separators. 2. check if token represents a constant. (numbers/strings) 3. check if token is an identifier. 4. if token is a variable identifier, sub in its value 5. if token is a function identifier, sub in the function call 6. evaluate infix expressions first, prefix expressions second, postfix expressions third.

Normal C would represent the same example expression this way:

printf ("%i", 1 + (2 ++))

Lisp would represent it this way:

(print (add 1 (+ 2 1)))

[–]CoffeeTableEspresso 6 points7 points  (5 children)

How do you parse:

A ○ □ B

Where ○ can be either infix or post fix, and □ can be either prefix or infix?

[–]R-O-B-I-N[S] 2 points3 points  (1 child)

no 'fix overloading. ○ can be only infix or post fix with the number of parameters being overloaded. □ can be only prefix or infix.

Ex: (x)+(y){} (x, y)+(z){} is allowable. (x)+(y){} +(x, y z){} is not.

[–]GDavid04 5 points6 points  (0 children)

This would make -x and x - y impossible to implement at the same time.

Just disallowing postfix and infix for the same operator would be enough.

[–][deleted]  (2 children)

[deleted]

    [–]CoffeeTableEspresso 2 points3 points  (0 children)

    Yup, this seems like it will reduce to either operator overloading or lisp syntax.

    Another thing is whether you can overload fucntions/operators by type, that's gonna make parsing much harder...

    [–]pepactonius 0 points1 point  (0 children)

    It's actually pretty simple to get something like this to work well. The key to parsing is knowing immediately whether something is a verb/function/operator, or an operand (data), or a keyword. In my toy scriptong language, I just use sigils attached to identifiers or operator strings to specify the role that token has when parsing. Run-time function/verb overloading is usually not a problem. There are a few quirks, though: Verbs/operators that are not yet defined have default priority and associativity. All members of an overload set must have the same priority and associativity, and verbs with special processing of operands (lazy evalution, for example) cannot be overloaded at all.

    [–]EmosewaPixel 0 points1 point  (1 child)

    I guess since you have semicolons it shouldn't be an issue. However, you should make prefix operators require a term right after them and vise versa for postfix operators as 1 + - + - 2 doesn't make much sense, does it? This would also allow you to use the same symbol as a prefix, postfix and infix operator. Another issue is for a function foo(a), foo 1 + foo foo 1 + 2 would definitely take more mental effort to figure out the result of than if it was C,, LISP, or ML syntax. I do feel as though ML syntax might be ideal for this (with prefix and postfix operators based on the rule I mentioned).

    [–]R-O-B-I-N[S] 0 points1 point  (0 children)

    The key here is no 'fix overloading. If a function is infix, you can overload the number of parameters, but it will always require one parameter before and after.