Skip to content

Commit

Permalink
Implemented xor, or, and and.
Browse files Browse the repository at this point in the history
Just emitting the correct tokens is enough to complete our math
operations.
  • Loading branch information
skx committed Jul 1, 2018
1 parent f7f7c7f commit 1b07816
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* [ ] Implement math operations: mod, pow, and, or
* [x] Implement math operations: `and`, `or`, `xor`
* [x] Implement string concat.
* [x] Implement system.
* [x] Implement peek/poke.
Expand Down
9 changes: 9 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ func (p *Compiler) Compile() {
case token.ADD:
p.mathOperation(opcode.ADD_OP)

case token.XOR:
p.mathOperation(opcode.XOR_OP)

case token.SUB:
p.mathOperation(opcode.SUB_OP)

Expand All @@ -208,6 +211,12 @@ func (p *Compiler) Compile() {
case token.DIV:
p.mathOperation(opcode.DIV_OP)

case token.AND:
p.mathOperation(opcode.AND_OP)

case token.OR:
p.mathOperation(opcode.OR_OP)

default:
fmt.Println("Unhandled token: ", p.curToken)

Expand Down
39 changes: 39 additions & 0 deletions examples/math.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

store #1, "255 & 16 is : "
print_str #1

store #1, 255
store #2, 16
and #0, #1, #2
print_int #0
store #1, "\n"
print_str #1

cmp #0, 16
jmpz and_ok

store #1, "Result is WRONG!\n"
print_str #1
exit

:and_ok

store #1, "32 XOR 23 is : "
print_str #1

# 32 XOR 23 => 55
store #1, 32
store #2, 23
xor #0, #1, #2
print_int #0
store #1, "\n"
print_str #1

cmp #0, 55
jmpz xor_ok

store #1, "Result is WRONG!\n"
print_str #1
exit

:xor_ok
18 changes: 12 additions & 6 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ const (

// math
ADD = "ADD"
SUB = "SUB"
MUL = "MUL"
AND = "AND"
DEC = "DEC"
DIV = "DIV"
INC = "INC"
DEC = "DEC"
MUL = "MUL"
OR = "OR"
SUB = "SUB"
XOR = "XOR"

// control-flow
CALL = "CALL"
Expand Down Expand Up @@ -91,11 +94,14 @@ var keywords = map[string]TokenType{

// math
"add": ADD,
"sub": SUB,
"mul": MUL,
"and": AND,
"dec": DEC,
"div": DIV,
"inc": INC,
"dec": DEC,
"mul": MUL,
"or": OR,
"sub": SUB,
"xor": XOR,

// control-flow
"call": CALL,
Expand Down

0 comments on commit 1b07816

Please sign in to comment.