Skip to content

Commit

Permalink
Finished assignment (for now).
Browse files Browse the repository at this point in the history
Waiting for feedback until I'll continue.
  • Loading branch information
JVKran committed May 21, 2021
1 parent 1afd081 commit 8d5b63c
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 34 deletions.
15 changes: 8 additions & 7 deletions compile/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def compileWhileNode(node: WhileNode, context: Context) -> None:
self.file.append(f"\tcmp \t{conditionIsMet.register}, #1\
\t\t@ Register {conditionIsMet.register} contains wether condition is met or not.\n")
self.file.append(f"\tbeq \tloop\
\t\t@ Branch to loop when condition is met.\n") # If condition is met; go back to loop label.
\t\t@ Branch to loop when condition is met.\n")

# compileForNode :: ForNode -> Context -> Nothing
def compileForNode(node: ForNode, context: Context) -> None:
Expand All @@ -226,20 +226,21 @@ def compileForNode(node: ForNode, context: Context) -> None:
stepNode = Number(1, None, context.registers.pop(0))
self.file.append(f"\tmovs\t{stepNode.register}, #1\n") # Step-size defaults to 1.

self.file.append("\t@ Should for-loop be entered?\n")
if stepNode.value >= 0:
self.file.append(f"\tcmp \t{endValue.register}, #{max(0, startValue.value - 1)}\n")
self.file.append(f"\tcmp \t{endValue.register}, #{max(0, startValue.value - 1)}\
\t@ Is iterator in valid range for entering of for-loop?\n")
self.file.append(f"\tble \tend\n")
else:
self.file.append(f"\tcmp \t{endValue.register}, #{max(0, startValue.value - 1)}\n")
self.file.append(f"\tcmp \t{endValue.register}, #{max(0, startValue.value - 1)}\
\t@ Is iterator in valid range for entering of for-loop?\n")
self.file.append(f"\tbgt \tend\n")

self.file.append("loop:\n")
self.compile(node.bodyNode, context)
self.file.append(f"\tadd \t{startValue.register}, {stepNode.register}\
\t@ Increment counter with stepsize.\n")
\t@ Increment counter ({startValue.register}) with stepsize ({stepNode.register}).\n")
self.file.append(f"\tcmp \t{startValue.register}, {endValue.register}\
\t@ Compare counter with value to iterate towards.\n")
\t@ Compare counter ({startValue.register}) with value to iterate towards ({endValue.register}).\n")

if stepNode.value >= 0:
self.file.append(f"\tble \tloop\n")
Expand Down Expand Up @@ -332,7 +333,7 @@ def compileReturnNode(node: ReturnNode, context: Context) -> Optional[Number]:
self.file.append("end:\n")
if res.register != "r0":
self.file.append(f"\tmovs\tr0, {res.register}\
\t@ Move contents of {res.register} to r0 for returning.\n")
@ Move contents of {res.register} to r0 for returning.\n")
return res

functionName: str = f'compile{type(node).__name__}' # Determine name of function to call.
Expand Down
7 changes: 7 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Usage
This example can be ran by executing ```make run``` after one has made sure the path to the [Bare-Metal Programming Toolkit](https://github.com/wovo/bmptk) and COM-port of the Arduino Due have been set according to your installation.

### Behaviour
The LED of the Arduino Due blinks fast when all tests were succesful and blinks slow when at least one test failed.

![Blinky GIF](./blink.gif)
Binary file added example/blink.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/source/even.asm
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ even:
movs r3, r0
.L4:
end:
movs r0, r3 @ Move contents of r3 to r0 for returning.
movs r0, r3 @ Move contents of r3 to r0 for returning.
pop { pc }
9 changes: 4 additions & 5 deletions example/source/fact.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ fact:
movs r1, #1 @ Register r1 contains 1.
movs r2, #2 @ Register r2 contains 2.
movs r3, #1
@ Should for-loop be entered?
cmp r0, #1
cmp r0, #1 @ Is iterator in valid range for entering of for-loop?
ble end
loop:
mul r1, r1, r2 @ Register r1 contains result from multiplication of registers r1 and r2.
add r2, r3 @ Increment counter with stepsize.
cmp r2, r0 @ Compare counter with value to iterate towards.
add r2, r3 @ Increment counter (r2) with stepsize (r3).
cmp r2, r0 @ Compare counter (r2) with value to iterate towards (r0).
ble loop
end:
movs r0, r1 @ Move contents of r1 to r0 for returning.
movs r0, r1 @ Move contents of r1 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/source/odd.asm
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ odd:
movs r3, r0
.L4:
end:
movs r0, r3 @ Move contents of r3 to r0 for returning.
movs r0, r3 @ Move contents of r3 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/source/summy.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ loop:
cmp r3, #1 @ Register r3 contains wether condition is met or not.
beq loop @ Branch to loop when condition is met.
end:
movs r0, r1 @ Move contents of r1 to r0 for returning.
movs r0, r1 @ Move contents of r1 to r0 for returning.
pop { r4, pc }
2 changes: 1 addition & 1 deletion example/tests/and_test.asm
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ and_test:
and r5, r5, r7
lsr r5, r5, #31 @ Register r5 contains wether r2 and r4 are both larger than 0.
end:
movs r0, r5 @ Move contents of r5 to r0 for returning.
movs r0, r5 @ Move contents of r5 to r0 for returning.
pop { r4, r5, r6, r7, pc }
2 changes: 1 addition & 1 deletion example/tests/division.asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ divide:
mov r2, r0 @ Store result of division in r2.
pop {r0, r1} @ Restore original values of r0 and r1.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/equals.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ equals:
neg r2, r3
adc r2, r2, r3 @ Register r2 contains wether r0 and r1 are equal.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/greater.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ greater:
movs r2, #0
.L2: @ Register r2 contains wether r0 is greater than r1.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/greater_equal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ greater_equal:
cmp r0, r1
adc r2, r2, r3 @ Register r2 contains wether r0 is greater than or equal to r1.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/if.asm
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ if_test:
movs r3, #0 @ Register r3 contains 0.
.L4:
end:
movs r0, r3 @ Move contents of r3 to r0 for returning.
movs r0, r3 @ Move contents of r3 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/less.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ less:
movs r2, #0
.L2: @ Register r2 contains wether r0 is less than r1.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/less_equal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ less_equal:
cmp r1, r0
adc r2, r2, r3 @ Register r2 contains wether r0 is less than or equal to r1.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
9 changes: 4 additions & 5 deletions example/tests/negative_for.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ neg_for:
movs r3, #0 @ Register r3 contains 0.
movs r4, #2 @ Register r4 contains 2.
sub r3, r4 @ Register r3 contains result from substraction of registers r3 and r4.
@ Should for-loop be entered?
cmp r2, #0
cmp r2, #0 @ Is iterator in valid range for entering of for-loop?
bgt end
loop:
add r1, r0 @ Register r1 contains result from addition of registers r1 and r0.
add r0, r3 @ Increment counter with stepsize.
cmp r0, r2 @ Compare counter with value to iterate towards.
add r0, r3 @ Increment counter (r0) with stepsize (r3).
cmp r0, r2 @ Compare counter (r0) with value to iterate towards (r2).
bge loop
end:
movs r0, r1 @ Move contents of r1 to r0 for returning.
movs r0, r1 @ Move contents of r1 to r0 for returning.
pop { r4, pc }
2 changes: 1 addition & 1 deletion example/tests/not_equals.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ not_equals:
sub r3, r2, #1
sbc r2, r2, r3 @ Register r2 contains wether r0 and r1 are not equal.
end:
movs r0, r2 @ Move contents of r2 to r0 for returning.
movs r0, r2 @ Move contents of r2 to r0 for returning.
pop { pc }
2 changes: 1 addition & 1 deletion example/tests/or_test.asm
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ or_test:
orr r5, r5, r7
lsr r5, r5, #31 @ Register r5 contains wether r2 or r4 is larger than 0.
end:
movs r0, r5 @ Move contents of r5 to r0 for returning.
movs r0, r5 @ Move contents of r5 to r0 for returning.
pop { r4, r5, r6, r7, pc }
21 changes: 17 additions & 4 deletions parse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def arithmeticExpression(tokenList : List[Token], tokenIndex : int) -> Tuple[int
def expression(tokenList : List[Token], tokenIndex : int) -> Tuple[int, Node]:
""" Parse an expression
This function parses an expression in the broadest term possible. Can be either of a variable,
if-statement, while-loop, function definition, function call or an operator.
if-statement, while-loop, for-loop, function definition, function call or an operator.
Parameters:
tokenList (List): The list with tokens to parse.
Expand Down Expand Up @@ -130,7 +130,7 @@ def expression(tokenList : List[Token], tokenIndex : int) -> Tuple[int, Node]:
# ifExpr :: [Token] -> Integer -> Tuple
def ifExpr(tokenList : List[Token], tokenIndex : int) -> Tuple[int, IfNode]:
""" Parse if-statement
This function parses an if-statement and (when given) also the else-expression.
This function parses an if-statement and (when provided) also the else-expression.
Parameters:
tokenList (List): The list with tokens to parse.
Expand Down Expand Up @@ -186,7 +186,20 @@ def whileExpr(tokenList : List[Token], tokenIndex : int) -> Tuple[int, WhileNode
tokenIndex = increment(tokenIndex, tokenList, FunctionEndToken)
return tokenIndex, WhileNode(condition, expr)

# forExpr :: [Token] -> Integer -> Tuple
def forExpr(tokenList : List[Token], tokenIndex : int) -> Tuple[int, ForNode]:
""" Parse for-loop
This function parses a for-loop with corresponding intial, end and optional
step-value.
Parameters:
tokenList (List): The list with tokens to parse.
tokenIndex (int): The current index at which we're parsing the tokenList.
Returns:
int: The incremented token index.
ForNode: A ForNode with the expression to execute as long as the condition is met.
"""
tokenIndex = increment(tokenIndex, tokenList, ForToken)
tokenIndex, startNode = variable(tokenList, tokenIndex)
tokenIndex = increment(tokenIndex, tokenList, ToToken)
Expand Down Expand Up @@ -255,9 +268,9 @@ def parseArguments(tokenIndex: int, tokenList: List[Token], separateToken: Token
tokenIndex, name = expression(tokenList, tokenIndex)
if type(tokenList[tokenIndex]) == FunctionParameterToken:
tokenIndex = increment(tokenIndex, tokenList)
if type(tokenList[tokenIndex]) == NowToken:
if type(tokenList[tokenIndex]) == NowToken: # Function call without parameters.
tokenIndex = increment(tokenIndex, tokenList)
else:
else: # Funcation call with parameters.
arguments = []
tokenIndex, intExpr = expression(tokenList, tokenIndex)
arguments.append(intExpr)
Expand Down

0 comments on commit 8d5b63c

Please sign in to comment.