Skip to content
Snippets Groups Projects
Verified Commit f8393156 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

adding more expr

parent 2a8ad4e6
No related branches found
No related tags found
No related merge requests found
Pipeline #34 passed with warnings with stages
in 4 minutes and 54 seconds
......@@ -150,4 +150,4 @@ Session.vim
### recolic
*.gi
*.log
package chocopy.common.analysis.types;
import java.util.Map;
import java.util.Objects;
import chocopy.common.astnodes.ClassType;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
/** Represents the semantic value of a simple class reference. */
......@@ -12,6 +14,10 @@ public class ClassValueType extends ValueType {
/** The name of the class. */
private final String className;
// recolic;
@JsonIgnore
public Map<String, SymbolType> memberMap = null;
/** A class type for the class named CLASSNAME. */
@JsonCreator
public ClassValueType(@JsonProperty String className) {
......
package chocopy.common.astnodes;
import java.util.Map;
import java.util.List;
import chocopy.common.analysis.NodeAnalyzer;
import chocopy.common.analysis.types.SymbolType;
import java_cup.runtime.ComplexSymbolFactory.Location;
/** A class definition. */
......@@ -15,6 +17,9 @@ public class ClassDef extends Declaration {
/** Body of the class. */
public final List<Declaration> declarations;
// Recolic:
public Map<String, SymbolType> memberMap = null;
/** An AST for class
* NAME(SUPERCLASS):
* DECLARATIONS.
......
......@@ -8,6 +8,10 @@ import chocopy.common.analysis.types.SymbolType;
import chocopy.common.analysis.types.ValueType;
import chocopy.common.astnodes.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 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.
......@@ -60,7 +64,22 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
@Override
public SymbolType analyze(ClassDef classDef) {
System.out.println("debug: classdef");
assert classDef.memberMap == null;
classDef.memberMap = new HashMap<>();
assert classDef.memberMap != null;
ClassValueType result_type = new ClassValueType(classDef.name.name);
SymbolTable<SymbolType> symLayer = new SymbolTable<>(sym);
symLayer.put("self", result_type);
sym = symLayer;
for(Declaration decl : classDef.declarations) {
String name = decl.getIdentifier().name;
SymbolType type = decl.dispatch(this);
classDef.memberMap.put(name, type);
}
sym = sym.getParent();
return new ClassValueType(classDef.name.name);
}
......@@ -72,9 +91,12 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
assert funcDef.symTable != null; // may suck if partial-compiling happens...
// Func parameter list
List<ValueType> args = new ArrayList<>();
for(TypedVar param : funcDef.params) {
String name = param.identifier.name;
sym.put(name, analyze(param));
ValueType type = ValueType.annotationToValueType(param.type);
sym.put(name, type);
args.add(type);
}
for(Declaration decl : funcDef.declarations) {
......@@ -87,7 +109,8 @@ public class DeclarationAnalyzer extends AbstractNodeAnalyzer<SymbolType> {
sym = sym.getParent();
if(sym == null)
throw new RuntimeException("logic error: sym parent is null while returning..");
return new FuncType(ValueType.annotationToValueType(funcDef.returnType));
FuncType funcT = new FuncType(args, ValueType.annotationToValueType(funcDef.returnType));
return funcT;
}
@Override
......
......@@ -2,10 +2,7 @@ package chocopy.pa2;
import chocopy.common.analysis.AbstractNodeAnalyzer;
import chocopy.common.analysis.SymbolTable;
import chocopy.common.analysis.types.FuncType;
import chocopy.common.analysis.types.ListValueType;
import chocopy.common.analysis.types.SymbolType;
import chocopy.common.analysis.types.ValueType;
import chocopy.common.analysis.types.*;
import chocopy.common.astnodes.*;
import java.util.*;
......@@ -247,6 +244,21 @@ public class TypeChecker extends AbstractNodeAnalyzer<SymbolType> {
}
}
@Override
public SymbolType analyze(MemberExpr node) {
ClassValueType objType = (ClassValueType) node.object.dispatch(this);
if(objType == null) {
err(node, ". operator left operand is not object type.");
return NONE_TYPE;
}
SymbolType resultType = objType.memberMap.get(node.member.name); // currently won't work. classDef not analyzed.
if(resultType == null) {
err(node, "undefined identifier " + node.member.name + " as object member.");
return NONE_TYPE;
}
return resultType;
}
@Override
public SymbolType analyze(Identifier id) {
String varName = id.name;
......
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