Skip to content
Snippets Groups Projects
Unverified Commit 98fcb13c authored by Recolic Keghart's avatar Recolic Keghart
Browse files

working on decl analyser

parent d16c4325
No related branches found
No related tags found
No related merge requests found
Pipeline #22 passed with warnings with stages
in 11 minutes and 20 seconds
...@@ -39,6 +39,8 @@ public class SymbolTable<T> { ...@@ -39,6 +39,8 @@ public class SymbolTable<T> {
/** Adds a new mapping of NAME -> VALUE to the current region, possibly /** Adds a new mapping of NAME -> VALUE to the current region, possibly
* shadowing mappings in the enclosing parent. Returns modified table. */ * shadowing mappings in the enclosing parent. Returns modified table. */
public SymbolTable<T> put(String name, T value) { public SymbolTable<T> put(String name, T value) {
if(declares(name))
throw new RuntimeException("SymbolTable: duplicate identifier " + name);
tab.put(name, value); tab.put(name, value);
return this; return this;
} }
......
...@@ -3,6 +3,8 @@ package chocopy.common.astnodes; ...@@ -3,6 +3,8 @@ package chocopy.common.astnodes;
import java.util.List; import java.util.List;
import chocopy.common.analysis.NodeAnalyzer; import chocopy.common.analysis.NodeAnalyzer;
import chocopy.common.analysis.SymbolTable;
import chocopy.common.analysis.types.SymbolType;
import java_cup.runtime.ComplexSymbolFactory.Location; import java_cup.runtime.ComplexSymbolFactory.Location;
/** Def statements. */ /** Def statements. */
...@@ -19,6 +21,9 @@ public class FuncDef extends Declaration { ...@@ -19,6 +21,9 @@ public class FuncDef extends Declaration {
/** Other statements. */ /** Other statements. */
public final List<Stmt> statements; public final List<Stmt> statements;
// Recolic
public SymbolTable<SymbolType> symTable = null;
/** The AST for /** The AST for
* def NAME(PARAMS) -> RETURNTYPE: * def NAME(PARAMS) -> RETURNTYPE:
* DECLARATIONS * DECLARATIONS
......
...@@ -2,14 +2,15 @@ package chocopy.pa2; ...@@ -2,14 +2,15 @@ package chocopy.pa2;
import chocopy.common.analysis.AbstractNodeAnalyzer; import chocopy.common.analysis.AbstractNodeAnalyzer;
import chocopy.common.analysis.SymbolTable; import chocopy.common.analysis.SymbolTable;
import chocopy.common.analysis.types.ClassValueType;
import chocopy.common.analysis.types.FuncType;
import chocopy.common.analysis.types.SymbolType; import chocopy.common.analysis.types.SymbolType;
import chocopy.common.analysis.types.ValueType; import chocopy.common.analysis.types.ValueType;
import chocopy.common.astnodes.*; import chocopy.common.astnodes.*;
import javax.management.RuntimeErrorException;
/** /**
* Analyzes declarations to create a top-level symbol table. * Analyzes declarations to create a top-level symbol table.
* Recolic: This class will ONLY fill the symTable. Every `Program` and `FuncDef` has a sym table.
*/ */
public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> { public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
...@@ -37,19 +38,14 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> { ...@@ -37,19 +38,14 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
String name = id.name; String name = id.name;
SymbolType type = decl.dispatch(this); SymbolType type = decl.dispatch(this);
System.out.println("decl type " + type.toString());
// recolic: what's the teacher willing to do???
if (type == null) { if (type == null) {
continue; continue;
} }
if (sym.declares(name)) { sym.put(name, type);
errors.semError(id,
"Duplicate declaration of identifier in same "
+ "scope: %s",
name);
} else {
sym.put(name, type);
}
} }
return null; return null;
...@@ -57,14 +53,45 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> { ...@@ -57,14 +53,45 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
@Override @Override
public SymbolType analyze(VarDef varDef) { public SymbolType analyze(VarDef varDef) {
System.out.println("debug");
return analyze(varDef.var); return analyze(varDef.var);
} }
@Override
public SymbolType analyze(ClassDef classDef) {
System.out.println("debug: classdef");
// TODO: check how do I create a obj type.
return new ClassValueType(classDef.name.name);
}
@Override
public SymbolType analyze(FuncDef funcDef) {
System.out.println("debug: start funcDef");
assert funcDef.symTable == null;
sym = funcDef.symTable = new SymbolTable<>(sym);
// Func parameter list
for(TypedVar param : funcDef.params) {
String name = param.identifier.name;
sym.put(name, analyze(param));
}
for(Declaration decl : funcDef.declarations) {
String name = decl.getIdentifier().name;
SymbolType type = decl.dispatch(this);
sym.put(name, type);
}
return new FuncType(ValueType.annotationToValueType(funcDef.returnType));
}
@Override @Override
public SymbolType analyze(TypedVar node) { public SymbolType analyze(TypedVar node) {
return ValueType.annotationToValueType(node.type); return ValueType.annotationToValueType(node.type);
} }
/*
@Override @Override
public SymbolType analyze(BinaryExpr node) { public SymbolType analyze(BinaryExpr node) {
String op = node.operator; String op = node.operator;
...@@ -130,6 +157,7 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> { ...@@ -130,6 +157,7 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
return result_type; return result_type;
} }
*/
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment