This repository has been archived on 2021-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
reemo-gasi/GASi/SymbolTable.cs
2021-09-20 03:05:36 +02:00

77 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GASi {
public enum kType {
STRING, INT, FLOAT, DATE, BOOL, VOID
}
class Conversion {
public static kType StringToType(string str) {
try {
return (kType)Enum.Parse(typeof(kType), str, true);
} catch {
throw Transpiler.Exception("Could not determine type implied by keyword '"+ str +"'", 0, 0);
}
}
}
class ScopeTable {
private List<Variable> Variables = new List<Variable>();
public void AddVariable(Variable var) {
if(!CheckVariableExists(var.Name))
Variables.Add(var);
else
throw Transpiler.Exception("Variable already exists with name '" + var.Name + "' in this scope", 0, 0);
}
public bool CheckVariableExists(string name) {
return Variables.FirstOrDefault(x => x.Name == name) != null;
}
public Variable GetVariable(string name) {
return Variables.FirstOrDefault(x => x.Name == name);
}
public class Variable {
public string Name { get; set; }
public kType Type { get; set; }
}
}
class MethodTable {
private List<Method> Methods = new List<Method>();
public void AddMethod(Method method) {
// TODO determine if GAS allows function overloading
if(!CheckMethodExists(method.Name))
Methods.Add(method);
else
throw Transpiler.Exception("Method already exists with name '" + method.Name + "'", 0, 0);
}
public bool CheckMethodExists(string name) {
return Methods.FirstOrDefault(x => x.Name == name) != null;
}
public Method GetMethod(string name) {
return Methods.FirstOrDefault(x => x.Name == name);
}
public class Method {
public string Name { get; set; }
public kType ReturnType { get; set; }
public List<Argument> Arguments { get; set; }
public ScopeTable Scope { get; set; } = new ScopeTable();
public class Argument {
public string Name { get; set; }
public kType Type { get; set; }
}
}
}
}