diff --git a/Compiler/.idea/.idea.Compiler/.idea/workspace.xml b/Compiler/.idea/.idea.Compiler/.idea/workspace.xml
index ebc02b9..c37df02 100644
--- a/Compiler/.idea/.idea.Compiler/.idea/workspace.xml
+++ b/Compiler/.idea/.idea.Compiler/.idea/workspace.xml
@@ -21,7 +21,10 @@
+
+
+
@@ -94,10 +97,8 @@
-
-
@@ -105,8 +106,10 @@
+
+
@@ -121,8 +124,8 @@
-
-
+
+
@@ -174,6 +177,7 @@
+
@@ -231,69 +235,85 @@
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
-
-
+
+
+
@@ -347,9 +367,10 @@
-
-
+
+
+
@@ -357,16 +378,16 @@
file://$PROJECT_DIR$/Parser/Parser.cs
- 261
-
+ 262
+
-
+
-
+
-
+
diff --git a/Compiler/Compiler.cs b/Compiler/Compiler.cs
index 0b2a1f9..a20c0bf 100644
--- a/Compiler/Compiler.cs
+++ b/Compiler/Compiler.cs
@@ -38,7 +38,7 @@ namespace Compiler
if (args.Length == 2)
{
- if (args[2] == "-v")
+ if (args[1] == "-v")
{
debug = true;
}
diff --git a/Compiler/DevFunctions.cs b/Compiler/DevFunctions.cs
index b2b6f8b..48ab883 100644
--- a/Compiler/DevFunctions.cs
+++ b/Compiler/DevFunctions.cs
@@ -104,28 +104,33 @@ namespace Compiler
public static void DevMode()
{
+ /*
for (int i = 3; i <= 3; i++)
{
Console.WriteLine($"---------------------valid, stage {i}-------------------------------");
foreach (string file in Directory.GetFiles($"/home/clemens/repositorys/lcc/stage_{i}/valid"))
{
- Console.WriteLine("-------------");
- List tokens = TestLexer(file, 0);
- Node programNode = TestParser(tokens, file, 1);
- //TestGenerator(programNode, 1);
- }
+ */
-
- Console.WriteLine($"---------------------invalid, stage {i}-------------------------------");
- foreach (string file in Directory.GetFiles($"/home/clemens/repositorys/lcc/stage_{i}/invalid"))
- {
- Console.WriteLine("-------------");
- List tokens = TestLexer(file, 0);
- Node programNode = TestParser(tokens, file, 1);
- //TestGenerator(programNode, 1);
- }
-
+ string file = "/home/clemens/repositorys/lcc/stage_3/valid/sub.c";
+
+ Console.WriteLine("-------------");
+ List tokens = TestLexer(file, 0);
+ Node programNode = TestParser(tokens, file, 1);
+ TestGenerator(programNode, 1);
+ //}
+
+ /*
+ Console.WriteLine($"---------------------invalid, stage {i}-------------------------------");
+ foreach (string file in Directory.GetFiles($"/home/clemens/repositorys/lcc/stage_{i}/invalid"))
+ {
+ Console.WriteLine("-------------");
+ List tokens = TestLexer(file, 0);
+ Node programNode = TestParser(tokens, file, 1);
+ //TestGenerator(programNode, 1);
}
+ */
+ //}
}
}
}
\ No newline at end of file
diff --git a/Compiler/Generator/Generator.cs b/Compiler/Generator/Generator.cs
index 84c2c4e..a413975 100644
--- a/Compiler/Generator/Generator.cs
+++ b/Compiler/Generator/Generator.cs
@@ -63,6 +63,44 @@ namespace Compiler.Generator
((UnaryOperatorNode) rootNode).OperatorType);
}
+ break;
+ case NodeType.BinaryOperatorNode:
+ switch (((BinaryOperatorNode) rootNode).OperatorType)
+ {
+ case OperatorType.Addition:
+ s = $"{Generate(rootNode.Children[0])}" +
+ "push %rax\n" +
+ $"{Generate(rootNode.Children[1])}" +
+ "pop %rcx\n" +
+ "add %ecx, %eax\n";
+ break;
+ case OperatorType.Subtraction:
+ s = $"{Generate(rootNode.Children[1])}" +
+ "push %rax\n" +
+ $"{Generate(rootNode.Children[0])}" +
+ "pop %rcx\n" +
+ "sub %ecx, %eax\n";
+ break;
+ case OperatorType.Multiplication:
+ s = $"{Generate(rootNode.Children[0])}" +
+ "push %rax\n" +
+ $"{Generate(rootNode.Children[1])}" +
+ "pop %rcx\n" +
+ "imul %rcx, %rax\n";
+ break;
+ case OperatorType.Division:
+ s = $"{Generate(rootNode.Children[0])}" +
+ "push %rax\n" +
+ $"{Generate(rootNode.Children[1])}" +
+ "movl %eax, %ecx" + //move calculated divisor to %ecx
+ "pop %rbx\n" + //pop divident do %ebx
+ "cdq\n" +
+ "idivl %ecx\n";
+ break;
+ default:
+ throw new ArgumentOutOfRangeException();
+ }
+
break;
default:
throw new NotSpecifiedException(rootNode.NodeType);
diff --git a/Compiler/Parser/Parser.cs b/Compiler/Parser/Parser.cs
index 636308a..f7760ae 100644
--- a/Compiler/Parser/Parser.cs
+++ b/Compiler/Parser/Parser.cs
@@ -32,7 +32,7 @@ namespace Compiler.Parser
}
//The main RDP function
- public Node Parse(NodeType nodeType)
+ public Node Parse(NodeType nodeType, bool unopPrecedence = false)
{
//declare node to be returned
Node n;
@@ -125,8 +125,9 @@ namespace Compiler.Parser
//if the next token is a + or a - it must be a binary operator because we are in an expression
//that means that this is not a plain constant but a BinaryOperator node
- while (expressionToken.TokenType == TokenType.AdditionToken ||
- expressionToken.TokenType == TokenType.NegationToken)
+ //also check if the last token was a unary operator
+ while ((expressionToken.TokenType == TokenType.AdditionToken ||
+ expressionToken.TokenType == TokenType.NegationToken) && !unopPrecedence)
{
//remove the - or + token
_tokenList.RemoveAt(0);
@@ -181,8 +182,8 @@ namespace Compiler.Parser
Token termToken = _tokenList[0];
//parse second factor if it exists
- while (termToken.TokenType == TokenType.MultiplicationToken ||
- termToken.TokenType == TokenType.DivisionToken)
+ while ((termToken.TokenType == TokenType.MultiplicationToken ||
+ termToken.TokenType == TokenType.DivisionToken) && !unopPrecedence)
{
_tokenList.RemoveAt(0);
switch (termToken.TokenType)
@@ -214,7 +215,7 @@ namespace Compiler.Parser
break;
-
+
case NodeType.FactorNode:
if (_tokenList.Count == 0)
{
@@ -224,7 +225,7 @@ namespace Compiler.Parser
Token factorToken = _tokenList[0];
//switch over possible next tokens. There are three possible options
-
+
switch (factorToken.TokenType)
{
//first option:
@@ -274,15 +275,15 @@ namespace Compiler.Parser
{
case TokenType.BitwiseComplementToken:
n = new UnaryOperatorNode(OperatorType.BitwiseComplement);
- n.Children.Add(Parse(NodeType.ExpressionNode));
+ n.Children.Add(Parse(NodeType.ExpressionNode, unopPrecedence = true));
break;
case TokenType.NegationToken:
n = new UnaryOperatorNode(OperatorType.Negation);
- n.Children.Add(Parse(NodeType.ExpressionNode));
+ n.Children.Add(Parse(NodeType.ExpressionNode, unopPrecedence = true));
break;
case TokenType.LogicalNegationToken:
n = new UnaryOperatorNode(OperatorType.LogicalNegation);
- n.Children.Add(Parse(NodeType.ExpressionNode));
+ n.Children.Add(Parse(NodeType.ExpressionNode, unopPrecedence = true));
break;
default:
throw new UnexpectedTokenException(TokenType.IntegerLiteralToken,