Skip to content

Commit

Permalink
Merge pull request #4 from dantheking-crypto/develop
Browse files Browse the repository at this point in the history
Finished part 10 of lsbasi
  • Loading branch information
danielzsh authored Oct 6, 2020
2 parents f6a3e2e + 2f20631 commit 7adb0b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
17 changes: 9 additions & 8 deletions Interpreter/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Interpreter
throw error;
}
}
double visit_Int(AstNode* node) {
double visit_Real(AstNode* node) {
if (node->print() == "Num") return visit_Num(*static_cast<Num*>(node));
else if (node->print() == "UnOp") return visit_UnOp(*static_cast<UnOp*>(node));
else if (node->print() == "Var") return visit_Var(*static_cast<Var*>(node));
Expand All @@ -32,19 +32,20 @@ class Interpreter
return num.value;
}
double visit_BinOp(BinOp binOp) {
if (binOp.op.type == Plus) return visit_Int(binOp.left) + visit_Int(binOp.right);
else if (binOp.op.type == Minus) return visit_Int(binOp.left) - visit_Int(binOp.right);
else if (binOp.op.type == Times) return visit_Int(binOp.left) * visit_Int(binOp.right);
else if (binOp.op.type == Div) return visit_Int(binOp.left) / visit_Int(binOp.right);
if (binOp.op.type == Plus) return visit_Real(binOp.left) + visit_Real(binOp.right);
else if (binOp.op.type == Minus) return visit_Real(binOp.left) - visit_Real(binOp.right);
else if (binOp.op.type == Times) return visit_Real(binOp.left) * visit_Real(binOp.right);
else if (binOp.op.type == Div) return (double)(visit_Real(binOp.left) / (double)visit_Real(binOp.right));
else if (binOp.op.type == IntDiv) return (int)(visit_Real(binOp.left) / visit_Real(binOp.right));
else {
std::string error("Error: BinOp operation not recognized.");
throw error;
}
}
double visit_UnOp(UnOp unOp) {
TokenType op = unOp.op.type;
if (op == Plus) return visit_Int(unOp.expr);
else return 0 - visit_Int(unOp.expr);
if (op == Plus) return visit_Real(unOp.expr);
else return 0 - visit_Real(unOp.expr);
}
void visit_Block(Block block) {
for (AstNode* child : block.children) {
Expand All @@ -53,7 +54,7 @@ class Interpreter
}
void visit_Assign(class Assign assign) {
string var_name = assign.var.value;
GLOBAL_SCOPE[var_name] = visit_Int(assign.right);
GLOBAL_SCOPE[var_name] = visit_Real(assign.right);
}
double visit_Var(Var var) {
if (GLOBAL_SCOPE.find(var.value) != GLOBAL_SCOPE.end()) return GLOBAL_SCOPE[var.value];
Expand Down
10 changes: 7 additions & 3 deletions Lexer/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,18 @@ class Lexer {
}
break;
case '/': {
char lookahead = (position + 1 < input.length()) ? input[position + 1] : '\0';
char lookahead = (position < input.length()) ? input[position] : '\0';
if (lookahead != '\0' && lookahead == '/') {
position++;
column++;
Token token(IntDiv, "//", line, column);
return token;
}
Token token(Div, "/", line, column);
return token;
else {
Token token(Div, "/", line, column);
return token;
}

}
break;
default:
Expand Down
7 changes: 4 additions & 3 deletions Parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Parser {
eat(Colon);
AstNode* node = parseStatement();
block.append(node);
while (currentToken.type == Semicolon) {
eat(Semicolon);
while (currentToken.type == Semicolon || node->print() == "Block") {
if (currentToken.type == Semicolon) eat(Semicolon);
node = parseStatement();
block.append(node);
}
Expand Down Expand Up @@ -118,10 +118,11 @@ class Parser {
}
AstNode* parseTerm() {
AstNode* node = parseFactor();
while (currentToken.type == Times || currentToken.type == Div) {
while (currentToken.type == Times || currentToken.type == Div || currentToken.type == IntDiv) {
Token token = currentToken;
if (token.type == Times) eat(Times);
else if (token.type == Div) eat(Div);
else if (token.type == IntDiv) eat(IntDiv);
node = new BinOp(node, token, parseFactor());
}

Expand Down
5 changes: 5 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ main() {
program:
a = 1*1+4/-2;
b = -13+4.5;
c = (a - b)//3;
{
program:
c = (a - b)//3;
}
}

0 comments on commit 7adb0b6

Please sign in to comment.