Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add booleans #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(self, ast):
"xor": self.instruction_xor,
"negate": self.instruction_negate,
"not": self.instruction_not,
"equal": self.instruction_equal,
"greater": self.instruction_greater,
"lesser": self.instruction_lesser,
"if": self.instruction_if,
"while": self.instruction_while,
"exit": lambda args, bindings: exit(self.resolve_value(args[0], bindings)),
Expand Down Expand Up @@ -112,7 +115,25 @@ def instruction_negate(self, args, bindings):
def instruction_not(self, args, bindings):
((_, target),) = args
old = bindings.resolve(target)
bindings.assign(target, 0 if old else 1)
bindings.assign(target, not old)

def instruction_equal(self, args, bindings):
((_, target),) = args
argument = self.stack.pop()
old = bindings.resolve(target)
bindings.assign(target, old == argument)

def instruction_greater(self, args, bindings):
((_, target),) = args
argument = self.stack.pop()
old = bindings.resolve(target)
bindings.assign(target, old > argument)

def instruction_lesser(self, args, bindings):
((_, target),) = args
argument = self.stack.pop()
old = bindings.resolve(target)
bindings.assign(target, old < argument)

def instruction_if(self, args, bindings):
for index in range(0, len(args) - 1, 2):
Expand Down Expand Up @@ -183,6 +204,9 @@ def resolve_value(self, value, bindings: Bindings):
if kind == "id":
return bindings.resolve(content)

if kind == "boolean":
return content == "true"

return content

def invoke_subroutine(self, name, bindings: Bindings):
Expand Down
22 changes: 16 additions & 6 deletions src/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
program = ws_0n subroutine (sp_0n br ws_0n subroutine)* ws_0n
subroutine = identifier ws_1n block
block = begin ws_1n (instruction (sp_0n br ws_0n instruction)*)? ws_1n end
instruction = conditional / loop / stack / op / io / stop
/ identifier / halt / store / load
instruction = conditional / loop / stack / op / io / stop / identifier
/ halt / store / load

conditional = if sp_1n value ws_1n block (ws_1n elif sp_1n value ws_1n block)* (ws_1n else ws_1n block)?
conditional = if sp_1n value ws_1n
block (ws_1n elif sp_1n value ws_1n block)*
(ws_1n else ws_1n block)?
loop = while sp_1n value ws_1n block
stack = (push sp_1n value) / (pop sp_1n identifier)
op = (add / subtract / multiply / divide / and / or / xor / negate / not) sp_1n identifier
op = (add / subtract / multiply / divide / and / or / xor
/ negate / not / equal / greater / lesser) sp_1n identifier
io = print sp_1n value
halt = exit sp_1n value

value = identifier / literal
literal = integer / float / string
literal = integer / float / string / boolean

identifier = !reserved ~r"[a-z][a-z0-9_]*"i

integer = ~r"-?(0|([1-9][0-9]*))"
float = ~r"-?(0|([1-9][0-9]*))\.[0-9]+"
string = quote string_text quote
boolean = true / false

quote = "\""
string_text = ~r"([^\"\\]|\\.)*"
Expand All @@ -35,7 +39,8 @@

reserved = begin / end / stop / if / elif / else / while / push
/ pop / add / subtract / multiply / divide / and
/ or / xor / not / print / exit / store / load
/ or / xor / not / print / exit / store / load / true
/ false / equal / greater / lesser
begin = ~r"begin"i / ~r"do"i
exit = ~r"exit"i
end = ~r"end"i
Expand All @@ -58,5 +63,10 @@
print = ~r"print"i
store = ~r"store"i
load = ~r"load"i
true = ~r"true"i
false = ~r"false"i
equal = ~r"equal"i
greater = ~r"greater"i
lesser = ~r"lesser"i
"""
)
5 changes: 4 additions & 1 deletion src/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def visit_io(self, _, visited_children):
def visit_stop(self, _, __):
return ("stop",)

def visit_halt(self, node, visited_children):
def visit_halt(self, _, visited_children):
operation, _, status_code = visited_children
return operation.text, status_code

Expand All @@ -89,6 +89,9 @@ def visit_integer(self, node, _):
def visit_float(self, node, _):
return "float", float(node.text)

def visit_boolean(self, node, _):
return "boolean", node.text

def visit_identifier(self, node, _):
return "id", node.text

Expand Down
24 changes: 15 additions & 9 deletions tests/add.imp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
main
begin
push -5
pop x
push 5
add x
exit x
exit 1
end
main begin
push 37
pop x

push 5
add x

push 42
equal x

if x do
exit 0
end
exit 1
end
4 changes: 2 additions & 2 deletions tests/not.imp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
main begin
push 1
push true
pop x
not x

if x do
exit 1
end

push 0
push false
pop x
not x

Expand Down
Loading