public static bool return_(SAR s, string scope) { string[] temp = scope.Split('.'); scope = "g"; if (temp.Length > 2) { scope += ("." + temp[1]); } string funcName = temp[temp.Length - 1]; //get the name of enclosing function for (int i = 0; i < ScopeTable[scope].Count; ++i) { if (ScopeTable[scope][i].value.Equals(funcName)) { string returnType = ScopeTable[scope][i].data[0].Split(':')[1]; if (s == null && returnType == "void") { return(true); } if (s.dataType == returnType) { s.dataType = returnType; return(true); } } } return(false); }
public static void LogicOperator() { SAR y = SAS.Pop(); SAR x = SAS.Pop(); SAR z = new SAR(SAR.SARtype.Identifier, x.token, x.value, x.scope); string op = OS.Peek(); if ((x.dataType != y.dataType) || (x.dataType != "bool")) { SemanticOperationError(x, y, OS.Pop()); } z.symid = "t" + uniqueCounter++; z.value += "_" + y.value; z.scope = "t"; z.dataType = "bool"; string[] data = { "returnType:" + z.dataType, "accessMod:private" }; Symbol temp = new Symbol("t", z.symid, z.value, "tvar", data); SymbolTable.Add(temp); if (op == "and") { ICode.AND(x.symid, y.symid, z.symid); } else if (op == "or") { ICode.OR(x.symid, y.symid, z.symid); } SAS.Push(z); OS.Pop(); OSprecidence.Pop(); return; }
public static void new_arr() { SAR exp = SAS.Pop(); SAR type = SAS.Pop(); if (exp.dataType != "int") { SemanticArrError(type, exp); } if (type.value == "null") { SemanticArrError(type, exp); } string[] data1 = { "returnType:int", "accessMod:private" }; Symbol size = new Symbol("t", "t" + uniqueCounter++, "t" + type.value, "tvar", data1); SymbolTable.Add(size); string[] data = { "returnType:" + "@" + type.value, "accessMod:private" }; Symbol temp = new Symbol("t", "t" + uniqueCounter++, "t" + type.value, "tvar", data); SymbolTable.Add(temp); type.symid = temp.symid; int classSize = SymbolTable.GetValue(type.symid).size; string[] sizeData = { "returnType:int", "accessMod:public" }; Symbol sizeLiteral = new Symbol("g", "N4", "4", "ilit", sizeData); SymbolTable.Add(sizeLiteral); ICode.MUL(sizeLiteral.symid, exp.symid, size.symid); ICode.NEW(size.symid, type.symid); type.dataType = "@" + type.value; SAS.Push(type); }
public static void AssignmentOperator() { SAR y = SAS.Pop(); SAR x = SAS.Pop(); if (x.dataType[0] == '@' && (x.argList != null && x.argList.Count > 0)) { x.dataType = x.dataType.Substring(1); } if (y.dataType[0] == '@' && (y.argList != null && y.argList.Count > 0)) { y.dataType = y.dataType.Substring(1); } if ((x.sarType == SAR.SARtype.Identifier || x.sarType == SAR.SARtype.Ref) && (x.dataType == y.dataType || y.value == "null" || y.dataType == "null")) { if ((y.value == "null" || y.dataType == null) && (x.dataType == "int" || x.dataType == "char" || x.dataType == "bool")) { } else { ICode.MOV(x.symid, y.symid); OS.Pop(); OSprecidence.Pop(); return; } } SemanticOperationError(x, y, OS.Peek()); }
public static void return_(Token t, string scope) { SAR expression = null; ShuntYardAll(); if (SAS.Count > 0) { expression = SAS.Pop(); if (expression.value == "true" || expression.value == "false") { expression.dataType = "bool"; } } if (!SymbolTable.return_(expression, scope)) { SemanticReturnError(expression, scope); } if (expression == null) { ICode.RTN(); } else { ICode.RETURN(expression.symid); } }
public static void func() { SAR al_sar = SAS.Pop(); SAR id_sar = SAS.Pop(); id_sar.argList = al_sar.argList; id_sar.sarType = SAR.SARtype.Function; SAS.Push(id_sar); }
public static void iExist() { SAR top_sar = SAS.Pop(); if (top_sar.value == "this") { SAS.Push(top_sar); return; } string args = ""; if (top_sar.argList.Count != 0) { args = top_sar.argList[0]; for (int i = 1; i < top_sar.argList.Count; ++i) { args += "," + top_sar.argList[i]; } } top_sar.paramType = args; string symid = SymbolTable.iExists(top_sar); if (symid == null || !SymbolTable.ContainsSymid(symid)) { SemanticError(top_sar); } Symbol symbol = SymbolTable.GetValue(symid); if (symbol.data != null) { top_sar.dataType = symbol.data[0].Split(':')[1]; top_sar.dataType.Trim(); } top_sar.paramType = symbol.parameters; top_sar.symid = symbol.symid; if (symbol.kind == "method") { ICode.FRAME(symbol.symid, "this"); if (symbol.parameters != null && symbol.parameters != "") { for (int i = 0; i < top_sar.argList.Count; ++i) { ICode.PUSH(top_sar.argList[i]); } } ICode.CALL(symbol.symid); Symbol temp = new Symbol("t", "t" + uniqueCounter++, "t_" + symbol.value + "_ReturnValue", "tvar", symbol.data); SymbolTable.Add(temp); top_sar.symid = temp.symid; if (symbol.data[0].Split(':')[1] != "void" && symbol.data[0].Split(':')[1] != "null") { ICode.PEEK(temp.symid); } } SAS.Push(top_sar); }
public static void MathOperator() { bool valid = false; SAR y = SAS.Pop(); SAR x = SAS.Pop(); SAR z = new SAR(SAR.SARtype.Identifier, x.token, x.value, x.scope); z.dataType = x.dataType; string op = OS.Peek(); if (x.dataType[0] == '@' && x.argList != null && x.argList.Count != 0) { if (x.dataType.Substring(1, x.dataType.Length - 1) == y.dataType) { valid = true; } } if (y.dataType[0] == '@' && y.argList != null && y.argList.Count != 0) { if (y.dataType.Substring(1, y.dataType.Length - 1) == x.dataType) { valid = true; } } if (((x.dataType == y.dataType) && (x.dataType == "int")) || valid) { z.symid = "t" + uniqueCounter++; z.value += "_" + y.value; z.scope = "t"; string[] data = { "returnType:" + z.dataType, "accessMod:private" }; Symbol temp = new Symbol("t", z.symid, z.value, "tvar", data); SymbolTable.Add(temp); if (op == "+") { ICode.ADD(x.symid, y.symid, z.symid); } else if (op == "-") { ICode.SUB(x.symid, y.symid, z.symid); } else if (op == "*") { ICode.MUL(x.symid, y.symid, z.symid); } else if (op == "/") { ICode.DIV(y.symid, x.symid, z.symid); } SAS.Push(z); OS.Pop(); OSprecidence.Pop(); return; } SemanticOperationError(x, y, OS.Peek()); }
public static void tExist() { SAR type_sar = SAS.Pop(); if (Scanner.typeKeyWords.Contains(type_sar.value)) { return; } if (!SymbolTable.tExists(type_sar)) { SemanticError(type_sar); } }
public static void EAL() { List <string> argList = new List <string>(); SAR top_sar; while ((top_sar = SAS.Pop()).sarType != SAR.SARtype.BAL) { argList.Add(top_sar.symid); } argList.Reverse(); top_sar = new SAR(SAR.SARtype.AL, null, null, null); top_sar.argList = argList; SAS.Push(top_sar); }
public static void arr() { SAR expression = SAS.Pop(); SAR identifier = SAS.Pop(); if (expression.dataType != "int") { SemanticArrError(identifier, expression); } identifier.argList = new List <string>(); identifier.argList.Add(expression.symid); identifier.paramType = "int"; identifier.dataType = "@"; SAS.Push(identifier); }
public static bool tExists(SAR s) { string scope = "g"; for (int i = 0; i < ScopeTable[scope].Count; ++i) { if (ScopeTable[scope][i].value.Equals(s.value)) { s.symid = ScopeTable[scope][i].symid; s.scope = scope; return(true); } } return(false); }
public static void cin() { ShuntYardAll(); SAR exp = SAS.Pop(); string type = exp.dataType; if ((type[0] == '@') && (exp.argList.Count > 0)) { type = type.Substring(1, type.Length - 1); } if (type != "char" && type != "int") { SemanticCoutError(exp); } ICode.READ(exp.symid); }
public static void if_(Token t) { if (SAS.Count == 0) { SemanticBoolError(t); } SAR expression = SAS.Pop(); if (expression.dataType != "bool") { SemanticBoolError(t); } string SKIPIF = (ICode.SKIPIF + ICode.labelCounter++) + " "; ICode.BF(expression.symid, SKIPIF); ICode.StackIf(SKIPIF); }
public static void while_(Token t) { if (SAS.Count == 0) { SemanticBoolError(t); } SAR expression = SAS.Pop(); if (expression.dataType != "bool") { SemanticBoolError(t); } string ENDWHILE = (ICode.ENDWHILE + ICode.labelCounter++) + " "; ICode.BF(expression.symid, ENDWHILE); ICode.StackEndWhile(ENDWHILE); }
public static void itoa() { SAR exp = SAS.Pop(); if (exp.dataType != "int") { SemanticAtoiError(exp); } string[] data = { "returnType:char", "accessMod:private" }; Symbol temp = new Symbol("t", "t" + uniqueCounter++, "t" + exp.value, "tvar", data); SymbolTable.Add(temp); ICode.MOV(temp.symid, exp.symid); ICode.CONVERT(temp.symid); exp.dataType = "char"; exp.symid = temp.symid; SAS.Push(exp); }
public static void NewObj() { SAR al = SAS.Pop(); SAR type = SAS.Pop(); if (!SymbolTable.NewObj(type, al.argList.ToArray())) { SemanticError(type); } string functionSymid = type.symid; type.sarType = SAR.SARtype.Ref; type.symid = "t" + uniqueCounter++; string[] data = { "returnType:" + type.dataType, "accessMod:private" }; Symbol instance = new Symbol("t", type.symid, "t" + type.value, "tvar", data); SymbolTable.Add(instance); string[] data2 = { "returnType:" + type.dataType, "accessMod:private" }; Symbol peekTemp = new Symbol("t", "t" + uniqueCounter++, "t" + type.value + "ReturnValue", "tvar", data2); SymbolTable.Add(peekTemp); Symbol staticInit = SymbolTable.GetValue(("Y" + functionSymid.Substring(1))); ICode.NEWI(type.size.ToString(), type.symid); //call constructor ICode.FRAME(functionSymid, type.symid); if (al.argList != null && al.argList.Count > 0 && al.argList[0] != "") { for (int i = 0; i < al.argList.Count; ++i) { ICode.PUSH(al.argList[i]); } } ICode.CALL(functionSymid); ICode.PEEK(peekTemp.symid); type.symid = peekTemp.symid; SAS.Push(type); }
public static Symbol rExists(SAR s) { Symbol temp = null; string args = ""; string scope = "g"; bool func = false; bool match = false; string[] parameters = { }; for (int i = 0; i < ScopeTable[scope].Count; ++i) { if (ScopeTable[scope][i].value.Equals(s.classType)) { scope += "." + s.classType; for (int j = 0; j < ScopeTable[scope].Count; ++j) { temp = ScopeTable[scope][j]; if (temp.kind == "Param") { continue; } if (temp.value == s.value) { match = true; s.symid = temp.symid; } else { continue; } if ((temp.kind == "method" || temp.kind == "constructor")) { func = true; match = false; if (temp.parameters == "") { if (s.argList.Count == 0) { s.symid = temp.symid; match = true; break; } continue; } args = ""; for (int q = 0; q < s.argList.Count; ++q) { if (q != 0) { args += ","; } args += symbolTable[s.argList[q]].data[0].Split(':')[1]; } parameters = temp.parameters.Split(','); if (s.argList.Count != parameters.Length)//method need to have matching number of parameters { continue; } Symbol definitionArg; Symbol passedArg; bool error = false; for (int p = 0; p < parameters.Length; ++p) { definitionArg = SymbolTable.GetValue(parameters[p]); passedArg = SymbolTable.GetValue(s.argList[p]); if (definitionArg.data[0] != passedArg.data[0]) { error = true; break; } } if (error) { continue; } s.symid = temp.symid; match = true; break; } else if (match == true) { break; } } if (!match) { if (func) { s.value += "(" + args + ")"; } s.scope = "class " + s.classType; return(null); } /*string symid = "t" + SemanticActions.uniqueCounter++; * Symbol ref_sar = new Symbol(s.scope, symid, s.value, "ref_sar", temp.data); * ref_sar.parameters = args; * symbolTable.Add(symid, ref_sar);*/ return(temp); } } s.scope = "class " + s.classType; if (func) { s.value += "("; for (int i = 0; i < s.argList.Count; ++i) { if (i != 0) { s.value += ","; } s.value += symbolTable[s.argList[i]].data[0].Split(':')[1]; } s.value += ")"; } s.value += " is"; return(null); }
public static string iExists(SAR s) { Symbol symbol; Symbol check; Symbol defined; bool flag = false; string tempScope = ""; string scope = s.scope; string parameterTypes = ""; int definedParamCount; while (scope != "") { if (ScopeTable.ContainsKey(scope)) { for (int i = 0; i < ScopeTable[scope].Count; ++i) { symbol = ScopeTable[scope][i]; if (symbol.value.Equals(s.value) && symbol.symid[1] != 'A') { tempScope = scope; if (symbol.parameters.Split(',')[0] != "") { definedParamCount = symbol.parameters.Split(',').Length; } else { definedParamCount = 0; } if (s.dataType == "@") { string type = symbol.data[0].Split(':')[1]; if (type.Length < 1 || type[0] != '@') { continue; } if (type[0] == '@') { type = type.Substring(1, type.Length - 1); } if (s.value == "i") { Console.Write(" "); } string symid = "tArr" + SemanticActions.uniqueCounter++; string[] data = new string[2]; data[0] = "dataType:" + type; data[1] = "accessMod:public"; Symbol temp = new Symbol("t", symid, "t" + s.value, "tvar", data); Add(temp); s.dataType = data[0]; s.symid = symid; s.scope = "t"; ICode.AEF(symbol.symid, s.argList[0], temp.symid); return(s.symid); } else if (s.argList.Count != definedParamCount) { for (int j = 0; j < s.argList.Count; ++j) { parameterTypes += symbolTable[s.argList[s.argList.Count - j - 1]].data[0].Split(':')[1] + ","; } continue; } else { parameterTypes = ""; for (int j = 0; j < s.argList.Count; ++j) { check = symbolTable[s.argList[j]];//args are in reverse order defined = symbolTable[symbol.parameters.Split(',')[j]]; if (check.data[0].Split(':')[1] != defined.data[0].Split(':')[1]) { flag = true; } parameterTypes += check.data[0].Split(':')[1] + ","; } if (flag) { continue; } } s.symid = ScopeTable[scope][i].symid; s.scope = scope; return(s.symid); } } } if (!scope.Contains(".")) { if (parameterTypes != null && parameterTypes.Length > 0) { s.value += "(" + parameterTypes.Substring(0, parameterTypes.Length - 1) + ")"; } if (tempScope != "") { s.scope = tempScope; } return(null); } string[] scopes = scope.Split('.'); scope = scopes[0]; for (int i = 1; i < scopes.Length - 1; ++i) { scope += "." + scopes[i]; } } s.value += "(" + parameterTypes.Substring(0, parameterTypes.Length - 1) + ")"; s.scope = tempScope; return(null); }
public static void rExist() { SAR top_sar = SAS.Pop(); SAR LHS = SAS.Peek(); bool this_ = false; string scope = top_sar.scope; if (SAS.Count > 0) { if (SAS.Peek().value != "this") { top_sar.classType = SAS.Pop().dataType; } else { this_ = true; string[] temp = top_sar.scope.Split('.'); top_sar.classType = temp[temp.Length - 1]; SAS.Pop(); } } else { SemanticError(top_sar); } Symbol symbol; if ((symbol = SymbolTable.rExists(top_sar)) == null) { SemanticError(top_sar); } if (symbol.data != null) //not a class { if ((symbol.data[symbol.data.Length - 1] == "accessMod:public") || this_) //member is public { top_sar.dataType = symbol.data[0].Split(':')[1]; top_sar.dataType.Trim(); top_sar.sarType = SAR.SARtype.Ref; top_sar.symid = "t" + uniqueCounter++; top_sar.scope = "t"; string[] data = { "returnType:" + top_sar.dataType, "accessMod:private" }; if (symbol.kind == "method") { ICode.FRAME(symbol.symid, LHS.symid); if (symbol.parameters != null && symbol.parameters != "") { for (int i = 0; i < top_sar.argList.Count; ++i) { ICode.PUSH(top_sar.argList[i]); } } ICode.CALL(symbol.symid); ICode.PEEK(top_sar.symid); } else { top_sar.symid += "r"; if (LHS.value == "this") { LHS.symid = "this"; } ICode.REF(LHS.symid, symbol.symid, top_sar.symid); } Symbol temp = new Symbol(top_sar.scope, top_sar.symid, "t" + top_sar.value, "tvar", data); SymbolTable.Add(temp); SAS.Push(top_sar); return; } else { SemanticPrivateError(top_sar); } } /*if (SymbolTable.ContainsValue(scope + "." + top_sar.value)) * { * Symbol symbol = SymbolTable.GetValue(SymbolTable.GetSymid(scope + "." + top_sar.value)); * if (symbol.data != null) //not a class * { * if (symbol.data[symbol.data.Length - 1]=="accessMod:public") //member is public * { * top_sar.dataType = symbol.data[0].Split(':')[1]; * top_sar.dataType.Trim(); * if (symbol.data.Length == 3) * { * top_sar.paramType = symbol.data[1]; * } * top_sar.sarType = SAR.SARtype.Ref; * SAS.Push(top_sar); * return; * } * } * }*/ SemanticError(top_sar); }
public static void SemanticItoaError(SAR sar) { Console.WriteLine(sar.token.lineNumber + ": " + sar.dataType + " cannot be converted to an int"); Environment.Exit(0); }
public static void BoolOperator() { SAR y = SAS.Pop(); SAR x = SAS.Pop(); SAR z = new SAR(SAR.SARtype.Identifier, x.token, x.value, x.scope); string op = OS.Peek(); if (op == "==" || op == "!=") { if ((x.dataType != y.dataType) && (y.dataType != "null")) { SemanticOperationError(x, y, OS.Pop()); } } else if ((x.dataType != y.dataType)) { /*if(x.dataType == "int" || x.dataType == "char") * { * * } * else * {*/ SemanticOperationError(x, y, OS.Pop()); // } } z.symid = "t" + uniqueCounter++; z.value += "_" + y.value; z.scope = "t"; z.dataType = "bool"; string[] data = { "returnType:" + z.dataType, "accessMod:private" }; Symbol temp = new Symbol("t", z.symid, z.value, "tvar", data); SymbolTable.Add(temp); if (op == "<") { ICode.LT(x.symid, y.symid, z.symid); } else if (op == ">") { ICode.GT(x.symid, y.symid, z.symid); } else if (op == "!=") { ICode.NE(x.symid, y.symid, z.symid); } else if (op == "==") { ICode.EQ(x.symid, y.symid, z.symid); } else if (op == "<=") { ICode.LE(x.symid, y.symid, z.symid); } else if (op == ">=") { ICode.GE(x.symid, y.symid, z.symid); } SAS.Push(z); OS.Pop(); OSprecidence.Pop(); return; }
public static void SemanticArrError(SAR identifier, SAR expression) { Console.WriteLine(identifier.token.lineNumber + ": " + identifier.value + "[" + expression.dataType + " " + expression.value + "] arr index requires type int"); Environment.Exit(0); }
public static void SemanticCinError(SAR sar) { Console.WriteLine(sar.token.lineNumber + ": cin requires char got " + sar.dataType); Environment.Exit(0); }
public static void SemanticReturnError(SAR s, string scope) { Console.WriteLine(s.token.lineNumber + ": " + s.dataType + " " + s.value + " does not match return type of " + scope); Environment.Exit(0); }
public static void SemanticOperationError(SAR x, SAR y, string symbol) { Console.WriteLine(x.token.lineNumber + ": Invalid Operation: " + x.dataType + " " + x.value + " " + symbol + " " + y.dataType + " " + y.value); Environment.Exit(0); }
public static void SemanticPrivateError(SAR sar) { Console.WriteLine(sar.token.lineNumber + ": " + sar.dataType + " " + sar.value + " is private and can't be accessed in " + sar.scope); Environment.Exit(0); }
public static bool NewObj(SAR s, string[] argsPassed) { Symbol definition; string[] argsDefined = { }; string scope = "g." + s.value; s.paramType = ""; for (int i = 0; i < ScopeTable["g"].Count; ++i) { if (ScopeTable["g"][i].value == s.value) { s.size = ScopeTable["g"][i].size; } } for (int i = 0; i < ScopeTable[scope].Count; ++i) { if (ScopeTable[scope][i].value.Equals(s.value)) { definition = ScopeTable[scope][i]; s.symid = definition.symid; if (definition.parameters != "" && definition.parameters != null) { argsDefined = definition.parameters.Split(','); } if (argsPassed.Length != argsDefined.Length) { continue; } bool flag = false; for (int j = 0; j < argsPassed.Length; ++j) { if (symbolTable[argsPassed[j]].data[0].Split(':')[1] != symbolTable[argsDefined[j]].data[0].Split(':')[1]) { flag = true; break; } if (j != 0) { s.paramType += ","; } s.paramType += symbolTable[argsPassed[j]].data[0].Split(':')[1]; } if (flag) { continue; } s.dataType = definition.data[0].Split(':')[1]; string className = scope.Split('.')[1]; foreach (Symbol classSymbol in ScopeTable["g"]) { if (classSymbol.value == className) { s.size = classSymbol.size; break; } } return(true); } } s.value += "(" + s.paramType + ")"; s.scope = scope; return(false); }
public static void SemanticError(SAR sar) { Console.WriteLine(sar.token.lineNumber + ": " + sar.dataType + " " + sar.value + " not defined in " + sar.scope); Environment.Exit(0); }