Indent tool for HSpeak

Talk about things that are not making games here. But you should also make games!

Moderators: Bob the Hamster, marionline, SDHawk

lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

As for the parser details, you certainly know better than me. The longest script I've ever written was 5 lines long and whatever I did was tailored to the reference material I've worked with.

I've added a function that removes all "none" nodes before attempting to compile because I realized I couldn't easily skip them at the last moment and also to support this syntax:

Code: Select all

HSpeak> a(10
 .... > ,20
 .... > ,30
 .... > )
flow: do
  function: a
    number: 10
    number: 20
    number: 30
It makes things easier for joining lines. The consequence is that:

Code: Select all

HSpeak> ,,,a,,,
flow: do
  value: a
But I think it's acceptable.

For speed you may want to consider partial compilaton. An hash can be created and stored for every script and they would be re-compiled as needed.

As for Python performance in general, the only tool that works for me is Shed Skin. I don't know if it works with PLY, but it's similar to how the Euphoria compiler works, and it comes with some interesting examples.

In any case, I advice you against trying to optimize Python code for speed by hand. It actually does nothing and you lose the benefit of readability.
Last edited by lennyhome on Wed Mar 18, 2020 5:31 pm, edited 6 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

I thought you were going to say that you removed none nodes in order to remove "variable" and "subscript", which can't be put in the hsz. They need to be removed so that the correct number of children is known for every node in compile_recurse.

I think a variant of your previous rule for concatenating lines, checking both whether the last char on the previous line and the first char of the new line are commas, could handle that better. Which I think is good enough to be a permanent solution.

Yes, I was actually considering (in HSpeak) hashing the source code of each script and only recompile the ones that changed. The tricky part is that it also needs to know which globals and constants and other scripts the script depends upon. Far too much trouble to be worth it. Oh... but in hspeak_parse, it's parsing/lexing that's slow, compiling to hsz is fast. And it's only compiling to hsz that depends on constants/globals/scripts, so a cache of lexer/parser output could be implemented really easily (this is Python afterall) and have a big impact.

I have plenty of experience trying to optimise Python, and I agree that it typically doesn't work. My tool of choice is Cython, but I'm not going to attempt that. I noticed that this compiler actually runs 3x slower under PyPy than CPython! PyPy has pathlogical performance when doing syscalls, including file I/O, so I tried loading all the files into memory... that barely helped. I guess this proves that PLY is highly optimised, for CPython, as it's pessimised for PyPy.
Last edited by TMC on Thu Mar 19, 2020 1:57 am, edited 2 times in total.
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

removed none nodes in order to remove "variable" and "subscript
I forgot about that. I knew the .hsz sizes don't match between compilers. Maybe that's the reason?

If you want to try some Python optimizations, you can get this. I wrote it a while ago. It loads Allegro 4 via ctypes and draws random 3D trees. Click and drag to change orientation and arrow keys to move. You can go crazy with cython on the matrix/vector classes.

----

In hspeak_parse I've split the "none" tag into "void" to indicate statements that are forbidden in arithmetic and "empty" to indicate and empty element.
In hspeak_ast I've added an AST_post function. For now it just interprets and removes "variable" calls but it could be used for more later.

----

I've made some changes to make it Python 2 compatible and I've written enough of the .hs generation code to be able to import scripts into the engine and make it execute something.

It shows the main menu in baconthulhu, then several errors about slices I don't know about. I was also able to compile and run my own script that uses strings and the keyboard.

I'm still missing "commands.bin" generation. For now I'm copying it from the original compiler. I have no idea what a "nonlocal" is and it also needs checks and re-factoring. But it works.
Last edited by lennyhome on Sun Mar 22, 2020 11:16 am, edited 8 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Oh, wow.
(You really should start making new posts instead of editing your previous post, I missed that update)
commands.bin is only used for certain error messages and is optional.

A bit surprised you went for Python 2 compatibility, but not unwelcome. I did just backport DWRL's Python 3 fixes to Python 2, but that was mostly because I think scons is normally installed as a Python 2 package (anyone who has compiled the OHRRPGCE successfully has it installed that way).

Nonlocal variable nodes are used when a subscript accesses variables from one of the scripts it's nested inside. (It's not possible to access variables in inner scripts, however it is possible to call a sibling subscript, or a sibling of the parent/grandparent/etc.
See https://rpg.hamsterrepublic.com/ohrrpgc ... l_Variable and https://rpg.hamsterrepublic.com/ohrrpgc ... riable_IDs
Admittedly the first section is very hard to understand, but the second has an example.

I fixed a bunch of bugs:
-return, continue, and break must have exactly one child (the script interpreter doesn't check this, so you get various strange errors due to reading garbage)
-the first child to 'for' must be a variable ID ('reference' symbols), not an expression (there's still no error checking for invalidly using a script or global @ reference)
-variable IDs for local variables were numbered wrong (see the link above if you care)
-math functions like 'random', 'not', 'sqrt' didn't compile

After that, I had to replace "end" with ")" in "place stairs up"/"place stairs down" to get past those scripts
That makes Baconthulhu run as far as calling "around x", which fails because that contains a 'switch' block. So I stopped there.
Last edited by TMC on Sun Mar 22, 2020 3:59 pm, edited 2 times in total.
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

Python2 compatibility is just because I could. Did you try my amazing demo game? I'm very proud of it and what's in the script source is pretty much what works currently.

I also need to add support for string literals. Since we're still in this weird quarantine I'll read all your suggestions and I'll update this post later.

----

Ok. I think I got most of your bugfixes in. I'll double check later but it's definitely working much better now. After this I'll be doing some re-factoring. Link is the same as usual.
Last edited by lennyhome on Sun Mar 22, 2020 6:50 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Oh, yes, very nice.

I think the biggest missing feature is default arguments for function and script calls (and argument count checking).

I'm amused that string_ref can be only a constant, not an integer literal, which is by far the most common case. Actually, it should be an arbitrary expression. Replacing 'string_ref' with 'expression' works beautifully except that it generates a conflict with "void : reference '=' expression" which breaks parsing of 'script' and 'plotscript'. But that rule for 'void' should not really exist anyway, rather, there would be a rule for 'script' and 'plotscript' header lines.

Really, when AST_state.build() is called from different contexts (outside any script, inside a script, defineconstant, etc) it should accept different grammars. But PLY only seems to let you set the start token when you build the parser. Should there be multiple parsers? Afterall, almost no grammar is shared between the different contexts except for commas separating tokens.

But keeping everything as a single parser/grammar could maybe be achieved by prefixing the input with a character telling the context, e.g.

Code: Select all

def p_root_toplevel(p):
    """
    root : '*' script_header
         | '*' expression_list
    """
    AST_state.root = p[2]

def p_root_script(p):
    "root : '(' expression_list"
    AST_state.root = AST_node("flow", p[2], "do")

def p_root_define_block(p):
    "root &#58; '<' expression_list"
    AST_state.root = p&#91;2&#93;

def p_script_header&#40;p&#41;&#58;
    "script_header &#58; name_concat ',' arg_list"
    p&#91;0&#93; = &#91;p&#91;1&#93;&#93; + p&#91;3&#93;

def p_arg_list1&#40;p&#41;&#58;
    """
    arg_list &#58; reference
             | default_value
    """
    p&#91;0&#93; = &#91;p&#91;1&#93;&#93;

def p_arg_list2&#40;p&#41;&#58;
    """
    arg_list &#58; arg_list ',' reference
             | arg_list ',' default_value
    """
    p&#91;0&#93; = p&#91;1&#93; + &#91;p&#91;3&#93;&#93;

def p_default_value&#40;p&#41;&#58;
    "default_value &#58; reference '=' expression"
    p&#91;0&#93; = AST_node&#40;"value", &#91;p&#91;3&#93;&#93;, p&#91;1&#93;.leaf&#41;
I almost have this working...


"return" without a condition isn't allowed; it would be pointless because "return" does not exit the script, unlike any other language with a "return" statement. In HS "exit returning" returns a value and exits (I've been meaning to add "exit" which is truly equivalent to "return" in other languages, with an optional return value. Trivial but I haven't.)

Any reason you removed/omitted support for the 4-argument form of 'for'?

"true" in HS is 1, not -1 (it's FB that uses -1). But I see plotscr.hsd will override those builtin definitions of true/false.

It had never really occurred to me how many things there are that HSpeak supports that aren't needed for the majority of scripts but add a lot of complexity. Stuff like definescript, defineoperator and plotscrversion; @obsolete; assert and tracevalue...
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

default arguments for function and script calls
That's already implemented. I think.
argument count checking
That's not implemented. Most error reporting aside from syntax errors is not implemented. And even then any error is just ignored. It's not a bug, it's an entirely new concept in programming. The idea is that you assert your superiority to the machine by ignoring its complaints.
string_ref can be only a constant, not an integer literal
I've just fixed it. Poorly.
Should there be multiple parsers?
You can add more parsers. My intent was to have a rough line oriented, preprocessor like parser and a proper one.
Any reason you removed/omitted support for the 4-argument form of 'for'?
No reason except for the fact that I'm not sure where I want to go. I think this may be a good time to fork the project if you want to retain or improve compatibility with existing scripts.

There is a lot that can be done with this framework to make the language more familiar for users. C-like operators, local variables introduced by assignment, string literals as arguments, arrays, all sorts of experimental features.

I don't know. For now I'm still in a phase where I'm surprised it does anything at all.

----

In a general sense there's an hard limit to how much of the behavior of an hand-written parser you can emulate with a machine-written one.
On the other hand, machine-written parsers have been so popular for so long that people assume a computer language is bound by their rules.
Last edited by lennyhome on Mon Mar 23, 2020 11:52 pm, edited 2 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

It's nice that it continues on an error, because otherwise you wouldn't get anything useful out of it.
Reading about doing error reporting with PLY.

I guess you're referring to the fact that default arguments are written to scripts.txt, but the script compiler needs to add defaults for missing arguments to script, function, and math operator calls itself; the interpreter doesn't do so. (Math ops: "increment"/"decrement" can be called with a single argument. Very uncommon, but certain people like to use that form)

In fact, the script interpreter doesn't even use the default args in scripts.txt even when it should (when a plotscript is triggered), and that's a bug.

You can add more parsers. My intent was to have a rough line oriented, preprocessor like parser and a proper one.
Right, that'll be a far cleaner solution.
No reason except for the fact that I'm not sure where I want to go. I think this may be a good time to fork the project if you want to retain or improve compatibility with existing scripts.

There is a lot that can be done with this framework to make the language more familiar for users. C-like operators, local variables introduced by assignment, string literals as arguments, arrays, all sorts of experimental features.
So you're planning to do more than just re-factoring, then?

I want to add most of that stuff to HS/HSpeak anyway.
Introducing variables by assignment (with special syntax like "x <- 2" or "var x := 2") is something I'm considering, I find "variable" annoyingly verbose. And some alternative/new operator spellings, like binary - instead of -- if possible (argh), unary -, != instead of <> and %% as an alternative to ,mod, which is absolutely awful. %% rather than % because I want to reserve % for units, eg 4% == 0.04. Incidentally I want to add other units too, like "walk hero(me, left, 40px)", or "2 tiles" or "wait(0.3s)". They'll compile to e.g. "px(40)". That would be very nice.

I do have a HSpeak branch where I added object.member and array[index] syntax and string literals, which compiled down to kludges like readglobal instead of real objects/types, arrays and strings. But I want to add the real things, after I get past these graphics features.

So if you want to work on these sorts of things, but still diverge from HS, it might be better to have two fork points, for HS-current and HS-future.
Last edited by TMC on Tue Mar 24, 2020 1:30 am, edited 1 time in total.
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

It's nice that it continues on an error
I've told you errors are overrated.
the script compiler needs to add defaults for missing arguments to script, function, and math operator calls itself
I suspected that. But I'm keeping track of the values, so it shouldn't be hard to do together with argument count checking in some AST_post function.
So you're planning to do more than just re-factoring, then?
We're still not allowed to go outside for another week at least. I have no idea what I'm going to do in the future and I don't want to think about it.

----

There's one thing worth noting that I did yesterday. I was't happy with how the "gen" and "hs" modules were attached to the "ast" module, so now I've attached them to "tld" instead. That solved most of the circular dependencies issues.

There's still a reason to let "parse" be attached to "ast" the way it is, but that has to do with how PLY works. All other imports are now just regular imports.

----

I've implemented default values and check for number of arguments for function and script calls. There's still no error reporting, but I do padding. Also, it was harder than expected, but I've got the engine to segfault reliably.

An important change I've made is that I finally I had it with using simple lists in the AST_state dictionaries and so I've introduced the AST_call_signature class which is meant to store what's needed to assemble a function or a script call.

After this and unless I forgot some other major feature, the general structure should remain stable for a while.

----

While implementing these latest changes I've noticed that sometimes functions are defined with constants as default values but some of those constants are defined after they're referenced.

After some panic, I've considered doing a third compiler pass, but then I've found another solution. Instead of trying to resolve the list of default arguments into a list of integers at the time of the function definition, I attach the relevant piece of the AST to the call signature and I leave it there unresolved.

Later in AST_post_3 I recall it and I merge it with the piece of AST that comes with the function call. From there the rest of the compiler does the resolution as it normally would. Since the function and constant definitions are read during pass 1, but the compiler operates during pass 2, they are resolved independent of the ordering.

The code that does this is not horribly complicated, but I thought it was worth describing. For scripts I do almost the same thing except for an extra step where I swap the parameters with their default values or with a zero.
Last edited by lennyhome on Tue Mar 24, 2020 10:06 pm, edited 9 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Yes, I mentioned that HSpeak does 3 passes, first for all the defineconstants and also definetrigger, second to process other top-level blocks once the constants and the names "script" and "plotscript" are known, and third to compile scripts after all script call signatures are known. But as you said, doing it that way isn't necessary provided no constants are used for "number of arguments" in definefunction. Your solution looks pretty clean to me.

A few functions like srunscriptbyid allow a variable number of arguments, which is indicted with n_args == -1 and no default values (it's too bad definefunction doesn't allow specifying a min or max number of args; I want to replace it at some point).
To accomodate that, you just need to add "if p_func.n_args == -1: continue" to AST_post_3().
Also, it was harder than expected, but I've got the engine to segfault reliably.
I don't know what you saw, but when I tried compiling and running baconthulhu.hss I got "encountered clean noop" (meaning it encountered the noop() function, which has id=0), which I traced back to random being miscompiled again. See fix in patch below.

I also noticed that ", and," etc binops weren't space- and case-insensitive, but the following generalisations of the regexps fix that.

Code: Select all

--- a/hspeak_gen.py
+++ b/hspeak_gen.py
@@ -83,7 +83,7 @@ def kind_and_id&#40;node&#41;&#58;
         # compatibility
         if node.leaf in binop_table&#58;
-            return KIND_FUNCTION, binop_table&#91;node.leaf&#93;
+            return KIND_MATH, binop_table&#91;node.leaf&#93;
         if node.leaf in unop_table&#58;
-            return KIND_FUNCTION, unop_table&#91;node.leaf&#93;
+            return KIND_MATH, unop_table&#91;node.leaf&#93;
         if node.leaf in flow_table&#58;
             return KIND_FLOW, flow_table&#91;node.leaf&#93;
@@ -91,6 +91,7 @@ def kind_and_id&#40;node&#41;&#58;
     if node.type == 'binop'&#58;
 
-        if node.leaf in binop_table&#58;
-            return KIND_MATH, binop_table&#91;node.leaf&#93;
+        canonical = node.leaf.upper&#40;&#41;.replace&#40;' ', ''&#41;
+        if canonical in binop_table&#58;
+            return KIND_MATH, binop_table&#91;canonical&#93;
 
     if node.type == 'unop'&#58;
--- a/hspeak_parse.py
+++ b/hspeak_parse.py
@@ -46,4 +46,4 @@ t_LT_EQUAL = r'<='
 t_GT_EQUAL = r'>='
-t_BITWISE_AND = r',AND,'
-t_BITWISE_OR = r',OR,'
+t_BITWISE_AND = r'&#40;?i&#41;,\s*AND\s*,'
+t_BITWISE_OR = r'&#40;?i&#41;,\s*OR\s*,'
 t_BOOL_AND = r'&&'
@@ -55,5 +55,5 @@ t_LESS_THAN = r'<<'
 t_GREATER_THAN = r'>>'
-t_BITWISE_XOR = r',XOR,'
+t_BITWISE_XOR = r'&#40;?i&#41;,\s*XOR\s*,'
 t_BOOL_XOR = r'\^\^'
-t_REMINDER = r',MOD,'
+t_REMINDER = r'&#40;?i&#41;,\s*MOD\s*,'
 
 
Last edited by TMC on Wed Mar 25, 2020 2:44 pm, edited 1 time in total.
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

I hope I got your patch in correctly this time.

Going forward I want to make this version (same link) final. It is close to what I had in my mind when I started and I think we've made something really interesting for people who like to play this computer language game.

Feel free to fork and re-license it however you want. It's our stuff. Sorry if you forked it before yesterday's marathon, but the call signature/default argument issue took me by surprise.

I think I want to do something else for a while now, but I'm pleased with how far we were able to go.
Last edited by lennyhome on Wed Mar 25, 2020 4:08 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

OK. Thank you hugely for your work. How do you want to be credited, just "Lenny"?

I'll commit it to the OHRRPGCE SVN repo (downloading and uploading the source is really not fun, why did we do that?) and continue working on it there, now that I know I won't be conflicting with your changes. You can get an svn account by asking James. I'll make the license dual-licensed as BSD or the OHRRPGCE license (currently GPL). In the absense of a name, I might pick 'physpeak'.

I'm currently working on implementing 'switch' and am sure I'll shortly manage to get the Baconthulhu scripts working 100%. (451 out of 498 scripts compile currently, and they all look good to me.) After that I'll try to get it to pass all the test cases, and have other improvements in mind too, and of course as a remote REPL for Game. Check back in a couple days.

This may yet become HSpeak v4. I'm considering the practicality of it, and also looked into possible alternatives to PLY (e.g. SLY, PlyPlus, PyBison, Lark) to see what our options are/whether there's any advantage. It does look pretty feasible to switch if there's a good reason, since they're so similar.
Last edited by TMC on Thu Mar 26, 2020 3:01 pm, edited 4 times in total.
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

Code: Select all

just "Lenny"?
That would be fine.

Code: Select all

get the Baconthulhu scripts working
I did it by converting all switch blocks to if/else blocks by hand. The only feature you can't run around is $msg + 10 = "". It generates the dungeon, the player moves, the menu works. There are glitches but it's almost playable.

Code: Select all

SLY, PlyPlus, PyBison, Lark
The reason I made it Python 2 compatible at some point was to try to compile the parser with ShedSkin. It didn't work. Lark looks interesting. I don't know. In my search for a parser generator I stopped at PLY because it was advertised as being close to flex/bison but without all the setup required for a C project.

Code: Select all

Check back in a couple days.
I'll be around unless the russian army decides to invade my home, which these days it's a possibility.

----

I've found a serious bug in AST_post_3. It's supposed to start like this:

Code: Select all

def AST_post_3&#40;node&#41;&#58;

    if node.children&#58;

        for child in node.children&#58;

            if \
            child.type != "function" and \
            child.type != "value"&#58;
                continue

            if child.leaf in AST_state.scripts&#58;
                p_func = AST_state.scripts&#91;child.leaf&#93;
            elif child.leaf in AST_state.functions&#58;
                p_func = AST_state.functions&#91;child.leaf&#93;
            else&#58;
                continue
The rest of the function is ok. I really don't know what happened there. I'm sure it was working at some point, then I must have hit undo one time too much.

----

It didn't occur to me initally but if you do this:

Code: Select all

def p_string_op_1&#40;p&#41;&#58;
    "void &#58; '$' string_ref '=' string_val"
    p&#91;0&#93; = AST_node&#40;"function", &#91;p&#91;2&#93;, p&#91;4&#93;&#93;, "setstringfromtable"&#41;
    
def p_string_op_2&#40;p&#41;&#58;
    "void &#58; '$' string_ref '+' string_val"
    p&#91;0&#93; = AST_node&#40;"function", &#91;p&#91;2&#93;, p&#91;4&#93;&#93;, "appendstringfromtable"&#41;
    
def p_string_op_3&#40;p&#41;&#58;
    "void &#58; '$' string_ref '+' expression"
    p&#91;0&#93; = AST_node&#40;"function", &#91;p&#91;2&#93;, p&#91;4&#93;&#93;, "concatenatestrings"&#41;
You can get rid of the "$a + b" vs. "a $+ b" syntax. It works and looks decent on code. Something like:

Code: Select all

$msg = "Drank a "
get item name&#40;tmp str, item&#41;
$msg + tmp str
$msg + ". &#40;"
append number&#40;msg, inventory&#40;item&#41;&#41;
$msg + " left&#41;"
----

I have a playable version of Baconthulhu here if you want to try it. I guess it's a demo for the new compiler and also a proposal for some language changes.
Last edited by lennyhome on Sun Mar 29, 2020 5:55 am, edited 10 times in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Very nice.
Yes, I suppose that's a pretty reasonable alternative to $+ and $=... however, is that going to prevent $msg+i="" from parsing?

A git mirror is here.
We've diverged, but I want to merge some of your changes back in, particularly the file reorganisation.

I added better error reporting and made the repl usable by importing readline. I can't stand repls that don't use at least readline. But I'd like to switch to something more powerful, maybe prompt_toolkit, and add suggestions, argument hinting, and more.

I've implemented 'switch', both old and new syntaxes, and I think it's compiled correctly but haven't tested it.

I've discovered the joy of writting LALR(1) grammars. Adding switch caused a lot of seemingly unrelated shift/reduce and even a reduce/reduce conflict to crop up. Spent a lot of time today wrapping my head around it and finally properly reading through the PLY docs.
In the quest to fix those I've removed the 'empty' symbol (not yet committed to svn) and am trying to find a better solution for handling newlines and commas (which removing 'empty' broke). Instead of 'empty' nodes, the difference between "x" and "x()" is indicated by .children being None or [].
Last edited by TMC on Sun Mar 29, 2020 8:34 am, edited 1 time in total.
lennyhome
Slime Knight
Posts: 115
Joined: Fri Feb 14, 2020 6:07 am

Post by lennyhome »

I'm so sorry about breaking compatibilty, but I just had to re-organize the operators and get rid of "--". I don't think it's going to be too hard to add some compatibility back later.
particularly the file reorganisation
I thought you would have liked to include it in your PyGTK tool at some point.
is that going to prevent $msg+i="" from parsing?
Yes. I've made it $(msg + i) = "" for expressions. It may be possible to do better by enstablishing the right priorities but I haven't put much effort into it.
I've implemented 'switch'
I've had a long debate with myself about it. I won't tell you the whole story, but I was considering doing constant propagation and having it would have gotten in the way.
children being None or []
In respect to what the PLY manual suggests, I've made some abuse of the fact that in Python both None and [] evaluate to False. So I haven't been too careful to differentiate them.

----

I was aware of a long-standing bug, which was due to the fact that I couln't find a negate function in the VM and knowing it was never used anyway by the original compiler, I had it temporarily aliased to the unary "not". So I've taken a page from your manual and did this:

Code: Select all

def p_unop&#40;p&#41;&#58;
    "expression &#58; '-' expression %prec UMINUS"
    if p&#91;2&#93;.type == "number"&#58;
        p&#91;0&#93; = AST_node&#40;"number", None, -p&#91;2&#93;.leaf&#41;
    else&#58;
        p&#91;0&#93; = AST_node&#40;"binop", &#91;p&#91;2&#93;, AST_node&#40;"number", None, -1&#41;&#93;, "*"&#41;
It's for stuff like:

Code: Select all

HSpeak> -a&#40;b&#41;
function&#58; do
  binop&#58; *
    function&#58; a
      value&#58; b
    number&#58; -1
Last edited by lennyhome on Sun Mar 29, 2020 11:54 pm, edited 7 times in total.
Post Reply