Handling neg vs sub

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Handling neg vs sub

pfonash
For Project 11, how do we handle the difference between neg and sub?  In figure 10.5 the "-" symbol is shown as an op and as an unary op.  In Project 10, I have unary ops taking precedence over ops.  I ran the text compare tool on all of my xml files and each was successful, but I don't think it tests for correctly handling neg?

In Project 11, however, I'm using a hashmap (a Python dictionary, specifically) to write the arithmetic calls. But, obviously, I can't have two entries for "-".  

I think I'm missing something.

Figure 10.5
 
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

cadet1620
Administrator
pfonash wrote
For Project 11, how do we handle the difference between neg and sub?  In figure 10.5 the "-" symbol is shown as an op and as an unary op.  In Project 10, I have unary ops taking precedence over ops.  I ran the text compare tool on all of my xml files and each was successful, but I don't think it tests for correctly handling neg?
You know the difference between unary negation and subtraction by context.  Unary negation can only occur as the first token in a term and subtraction can only occur as the token between two terms in an expression.

If you are using recursive decent parsing, this happens directly because parseExpression() will match '-' between terms as the subtraction operator, and parseTerm() will match '-' as the unary negation operator.

This is similar to the way that '=' matches different operations in
    let x=3;
and
    if (x=3) {

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

pfonash
Got it.  Thanks, Mark.
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

cadet1620
Administrator
Glad to have helped.  FWIW, I wrote my n2t tools in Python, too.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

pfonash
Interesting.  I would not have guessed that.  For some reason, I pictured you writing your n2t tools in C.
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

cadet1620
Administrator
In my job as an embedded systems programmer I do a lot of C.

The year before I discovered the n2t course I had taught a class of gifted 12- and 13-year-olds (and myself!) how to program using Python. I decided that n2t provided the perfect excuse to more deeply explore it. Python has become a regular utility language for me. I like that it's interactive and all the extensions available for it.

I've also written the Assembler in Ruby to explore that language when a student on the forum was talking it up, but it didn't grab me like Python did.

I was working with a student who was fixated on writing his tools in ANSI C and was trying to explain the difficulties he would have dealing with the symbol tables given the lack of built in container types and decided I'd give it a shot in C. I cheated (be sure always to call it please "research"*) and grabbed a hash table library from the net. Since the library I found included dynamic arrays, I could easily buffer the generated code so I write it as a one pass assembler. The result is blazingly fast compared to the Python version.

My latest version of the Assembler is written in Jack. I still need to write a post about it... (See this post about my Jack Compiler written in Jack.)

--Mark
__________
*  Tom Lehrer, Lobachevsky
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

pfonash
Ah, gotcha.  I like Python, too, but I try not to use it that often because it's so good it makes me lazy.

Um, that's awesome.  Definitely need to write a post about it!
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

ivant
pfonash wrote
Ah, gotcha.  I like Python, too, but I try not to use it that often because it's so good it makes me lazy.
Nonsense! Laziness is one of the three virtues of programmers!
Reply | Threaded
Open this post in threaded view
|

Re: Handling neg vs sub

gnb03
In reply to this post by cadet1620
Hi Cadet1620,

I’m finishing up Chapter 12 of Nand2Tetris as I write this. Taking Nand2Tetris has sparked my interest in embedded systems and robotics.

Since you mentioned that you work on embedded systems, I was wondering if you could recommend some introductory books or online courses about embedded systems to a novice programmer who has (almost) completed Nand2Tetris.

Some background about me: I studied mechanical engineering as an undergraduate. Aside from Nand2Tetris, I don’t have much programming experience. I’ve heard from a few people that C is commonly used for embedded systems and robotics and I plan to take a an introductory course in C after I finish Nand2Tetris. However, I don’t know how I would delve deeper into embedded systems after that.

Best,

Jason