Permalink
Browse files

Merge branch 'master' of github.com:PLTteam22/SmarTest

  • Loading branch information...
2 parents 34dd00b + dc84192 commit 99ca7996ff71f7ea1e8eed6df26000d98726b2cf @harpreet2301 harpreet2301 committed Apr 10, 2012
Showing with 148 additions and 7 deletions.
  1. +105 −1 src/AssignmentOperatorNode.java
  2. +31 −0 src/FunctionNode.java
  3. +6 −5 src/ProgramNode.java
  4. +6 −1 src/stl.y
@@ -1,7 +1,111 @@
+/*
+ * Implements semantic checking and output code generation
+ * of assignment operator ( = )
+ * Example: x = y * d
+ */
+public class AssignmentOperatorNode extends ASTNode {
-public class AssignmentOperatorNode {
+ private ASTNode idType, id, expr;
+ private boolean isDeclaration = false;
+
+ /*
+ * Instantiates AssignmentOperatorNode invoked by this grammar:
+ * type ID '=' expression. Note this contains a declaration as well.
+ *
+ * Example:
+ * int x = 5
+ *
+ * @param idType represents the type of the identifier (e.g. int)
+ * @param id represents the identifier node
+ * @param expr represents the right-hand side of the operator
+ */
+ public AssignmentOperatorNode(ASTNode idType, ASTNode id, ASTNode expr, int yyline, int yycolumn) {
+ super(yyline, yycolumn);
+ this.addChild(idType);
+ this.addChild(id);
+ this.addChild(expr);
+ this.setDeclaration(true);
+ this.isDeclaration = true;
+ this.idType = idType;
+ this.id = id;
+ this.expr = expr;
+ }
+
+
+ /*
+ * Instantiates AssignmentOperatorNode invoked by this grammar:
+ * ID '=' expression
+ *
+ * Example:
+ * x = 5
+ *
+ * @param idType represents the type of the identifier (e.g. int)
+ * @param id represents the identifier node
+ * @param expr represents the right-hand side of the operator
+ */
+ public AssignmentOperatorNode(ASTNode id, ASTNode expr, int yyline, int yycolumn) {
+ super(yyline, yycolumn);
+ this.addChild(idType);
+ this.addChild(id);
+ this.addChild(expr);
+ this.isDeclaration = false;
+ this.id = id;
+ this.expr = expr;
+ }
+
+ /*
+ * Verifies that right side type matches the type of the identifier as in the symbol table
+ * @see ASTNode#checkSemantics()
+ */
+ @Override
+ public void checkSemantics() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public String generateCode() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+
+ public ASTNode getIdType() {
+ return idType;
+ }
+
+ public void setIdType(ASTNode idType) {
+ this.idType = idType;
+ }
+
+ public ASTNode getId() {
+ return id;
+ }
+
+ public void setId(ASTNode id) {
+ this.id = id;
+ }
+
+ public ASTNode getExpr() {
+ return expr;
+ }
+
+ public void setExpr(ASTNode expr) {
+ this.expr = expr;
+ }
+
+
+ public boolean isDeclaration() {
+ return isDeclaration;
+ }
+
+
+ public void setDeclaration(boolean isDeclaration) {
+ this.isDeclaration = isDeclaration;
+ }
}
View
@@ -0,0 +1,31 @@
+package smartest;
+
+import java.util.ArrayList;
+
+public class FunctionNode extends ASTNode
+{
+
+ public FunctionNode(String id, ArrayList<ASTNode> paramList, ArrayList<ASTNode> statementList, int yycol)
+ {
+ super(yyline, yycol);
+ this.children.addAll(functionList);
+ }
+ public void semanticCheck()
+ {
+ for (param : paramList)
+ {
+ param.semanticCheck();
+ }
+ for (statement : statementList)
+ {
+ statement.semanticCheck();
+ }
+ if (Parser.getFunctionSymbolTable().contains(id.toLowerCase()))
+ {
+ throw new Exception("Line " + this.getYyline() +
+ ": Function " + id.toLowerCase() +
+ " is already defined");
+ }
+ }
+
+}
View
@@ -11,15 +11,16 @@ public ProgramNode(ArrayList<ASTNode> functionList, int yyline, int yycol)
this.children.addAll(functionList);
}
public void semanticCheck()
- {
- if (!((FunctionNode)this.children.getChildAt(0)).getIdentifier().equalsIgnoreCase("main"))
- {
- throw new Exception("No function called main defined");
- }
+ {
for (function : this.children)
{
function.semanticCheck();
}
+ if (!((FunctionNode)this.children.getChildAt(0)).getIdentifier().equalsIgnoreCase("main"))
+ {
+ throw new Exception("No function called main defined");
+ }
+
}
}
View
@@ -1,6 +1,7 @@
%{
import java.io.*;
+import java.util.HashMap;
%}
@@ -37,7 +38,7 @@ statements : statements statement { System.out.print("found statements\n"); }
/* statement productions go here */
statement : type ID ';' { System.out.print("found statement (int i;)\n"); }
-| type ID '=' expression ';' { System.out.print("found statement (int i=5;)\n"); }
+| type ID '=' expression ';' { System.out.print("found statement (int i=5;)\n"); $$ = new ParserVal(new AssignmentOperatorNode($1.obj, $2.obj, $4.obj, yyline, yycolumn)); }
| ID '=' expression ';' { System.out.print("found statement (i=5;)\n"); }
| function_call ';' { System.out.print("found statement (func call)\n"); }
| loop { System.out.print("found statement (loop)\n"); }
@@ -144,6 +145,10 @@ factor_list: factor_list ',' factor {
* Variables
***************************************/
private Yylex lexer;
+private static int yyline, yycolumn;
+public static HashMap<String, String[]> symbolsTable;
+public static HashMap<String, String[]> functionSymbolsTable;
+
/**************************************

0 comments on commit 99ca799

Please sign in to comment.