void AddSymbolToScope(string key, Type type, Kind kind, dynamic value = null, int pos = -1) { SymbolTable table; if (currentScope.Length == 0) { table = symbolTable; } else { table = procedureTable[currentScope].symbols; } table[key] = new SymbolTable.Row(type, kind, pos, value); }
private string GetCilValue(VariableType variable) { StringBuilder result = new StringBuilder(); int index = 0; SymbolTable.Row row = variable.Value; switch (row.type) { case Type.BOOL: var b = row.value ? 1 : 0; return($"ldc.i4.{b}"); case Type.INT: return($"ldc.i4 {row.value}"); case Type.STRING: string s = row.value; s = s.Count() == 0 ? "\"\"" : s; return($"ldstr {s}"); case Type.BOOL_LIST: bool[] constBoolArr = row.value as bool[]; result.AppendLine($"ldc.i4 {constBoolArr.Length}"); result.AppendLine("\t\tnewarr int32"); foreach (bool val in constBoolArr) { result.AppendLine($"\t\tdup"); result.AppendLine($"\t\tldc.i4 {index++}"); result.AppendLine($"\t\tldc.i4.{(val ? 1 : 0)}"); result.AppendLine($"\t\tstelem.i4"); } return(result.ToString()); case Type.INT_LIST: int[] constIntArr = row.value as int[]; result.AppendLine($"ldc.i4 {constIntArr.Length}"); result.AppendLine("\t\tnewarr int32"); foreach (int val in constIntArr) { result.AppendLine($"\t\tdup"); result.AppendLine($"\t\tldc.i4 {index++}"); result.AppendLine($"\t\tldc.i4 {val}"); result.AppendLine($"\t\tstelem.i4"); } return(result.ToString()); case Type.STRING_LIST: string[] constStrArr = row.value as string[]; result.AppendLine($"ldc.i4 {constStrArr.Length}"); result.AppendLine("\t\tnewarr string"); foreach (string val in constStrArr) { result.AppendLine($"\t\tdup"); result.AppendLine($"\t\tldc.i4 {index++}"); result.AppendLine($"\t\tldstr {val}"); result.AppendLine($"\t\tstelem.ref"); } return(result.ToString()); case Type.VOID: return(""); } throw new Exception($"Could not find value for: {variable.Key}"); }