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

Remove assigments instructions #23

Merged
merged 1 commit into from
Jan 3, 2025
Merged
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
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describes the language's features, syntax, and usage.
- **Control Flow**: Conditional branching (`if`, `elif`, `else`) and loops (`while`).
- **Arithmetic Operations**: Basic operations like addition, subtraction, multiplication, and division.
- **Logic Operations**: Basic logic operations (`and`, `or`, `xor`,`not`, `negate`)
- **Variables**: Assign and use named variables (`let`).
- **Input/Output**: Print strings or variable values.
- **Modularity**: Define subroutines for reusable code.
- **Comments**: Supports comments anywhere in the code using `#`
Expand Down Expand Up @@ -41,16 +40,7 @@ end

### Instructions

Instructions include variable assignment, stack operations, arithmetic operations, and control flow constructs.

#### Variable Assignment

Assign a value to a variable:

```imperivm
push 42
let x
```
Instructions include variable stack operations, arithmetic operations, and control flow constructs.


#### Memory Operations
Expand Down
7 changes: 0 additions & 7 deletions src/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ def instruction_pop(self, args, bindings):
value = self.stack.pop()
bindings.assign(target, value)

def instruction_let(self, args, bindings):
[(_, target)] = args
value = self.stack.pop()
bindings.assign(target, value)

def instruction_invocation(self, args):
_, subroutine = args[0]
self.invoke_subroutine(subroutine, Bindings())
Expand Down Expand Up @@ -177,8 +172,6 @@ def execute_instruction(self, instruction, bindings: Bindings):
self.instruction_push(args, bindings)
elif operation == "pop":
self.instruction_pop(args, bindings)
elif operation == "let":
self.instruction_let(args, bindings)
elif operation == "invocation":
self.instruction_invocation(args)
elif operation == "stop":
Expand Down
6 changes: 2 additions & 4 deletions src/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
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 = assignment / conditional / loop / stack / op / io / stop
instruction = conditional / loop / stack / op / io / stop
/ identifier / halt / store / load

assignment = let sp_1n identifier
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)
Expand All @@ -35,7 +34,7 @@
sp_1n = ~r"[ \t]+"

reserved = begin / end / stop / if / elif / else / while / push
/ pop / let / add / subtract / multiply / divide / and
/ pop / add / subtract / multiply / divide / and
/ or / xor / not / print / exit / store / load
begin = ~r"begin"i / ~r"do"i
exit = ~r"exit"i
Expand All @@ -47,7 +46,6 @@
while = ~r"while"i
push = ~r"push"i
pop = ~r"pop"i
let = ~r"let"i
add = ~r"add"i
subtract = ~r"subtract"i
multiply = ~r"multiply"i
Expand Down
6 changes: 1 addition & 5 deletions src/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ def visit_block(self, _, visited_children):
result.insert(0, first)
return tuple(result)

def visit_instruction(self, node, visited_children):
def visit_instruction(self, _, visited_children):
if visited_children[0][0] == "id":
return [("invocation", visited_children[0])]
return visited_children

def visit_assignment(self, _, visited_children):
instruction, _, target = visited_children
return instruction.text, target

def visit_conditional(self, _, visited_children):
operation, _, condition, _, block, elif_operations, else_operations = visited_children

Expand Down
2 changes: 1 addition & 1 deletion tests/add.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push -5
let x
pop x
push 5
add x
exit x
Expand Down
2 changes: 1 addition & 1 deletion tests/and.imp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main begin
push 1
let x
pop x
push 2
and x

Expand Down
2 changes: 1 addition & 1 deletion tests/divide.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push 0
let x
pop x
push 14884
divide x
exit x
Expand Down
2 changes: 1 addition & 1 deletion tests/if.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push 1
let x
pop x
if x
begin
exit 0
Expand Down
4 changes: 2 additions & 2 deletions tests/if_elif.imp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
main
begin
push 0
let x
pop x
push 1
let y
pop y
if x
begin
exit 1
Expand Down
2 changes: 1 addition & 1 deletion tests/if_elif_else.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push 0
let x
pop x
if x
begin
exit 1
Expand Down
2 changes: 1 addition & 1 deletion tests/if_else.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push 0
let x
pop x
if x
begin
exit 1
Expand Down
2 changes: 1 addition & 1 deletion tests/multiply.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push 84781
let x
pop x
push 0
multiply x
exit x
Expand Down
2 changes: 1 addition & 1 deletion tests/negate.imp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main begin
push 1
let x
pop x
negate x

push 2
Expand Down
2 changes: 1 addition & 1 deletion tests/not.imp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main begin
push 1
let x
pop x
not x

if x do
Expand Down
2 changes: 1 addition & 1 deletion tests/or.imp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main begin
push 1
let x
pop x
push 2
or x

Expand Down
2 changes: 1 addition & 1 deletion tests/subtract.imp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
main
begin
push 5
let x
pop x
push 5
subtract x
exit x
Expand Down
2 changes: 1 addition & 1 deletion tests/xor.imp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main begin
push 1
let x
pop x
push 3
xor x

Expand Down
Loading