Skip to content

Commit

Permalink
- Iniciada a adição de suporte à arrays dinâmicos, estes também serão…
Browse files Browse the repository at this point in the history
… contados por referência assim como as strings.

- Iniciado a adição de suporte à classes, interfaces, enumerações e uniões. Instâncias de classes ou interfaces também serão contadas por referências assim como as strings e arrays dinâmicos.

- Strings e arrays dinâmicos (ambos contados por referência) serão todos classificados como objetos, da mesma forma que as instâncias de classes/interfaces.

- Modificado a forma como as informações de depuração sobre variáveis locais são armazenadas para se basearem em escopos baseados em faixas de ip ao invés de intervalos de linhas.

- Alterada a palavra chave "externa" para "esterna".

- Adicionado diversas outras palavras chaves sem função pre-definida ainda, mas que futuramente serão associadas a novas funções.
  • Loading branch information
sharivan committed Sep 7, 2023
1 parent ec9001e commit 27f3d11
Show file tree
Hide file tree
Showing 27 changed files with 576 additions and 311 deletions.
47 changes: 30 additions & 17 deletions Comp/CompilationUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public enum ImportResult
private readonly Dictionary<string, CompilationUnity> importTable;
private readonly List<GlobalVariable> globals;
private readonly Dictionary<string, GlobalVariable> globalTable;
private readonly List<StructType> structs;
private readonly Dictionary<string, StructType> structTable;
private readonly List<FieldAggregationType> fieldAggregations;
private readonly Dictionary<string, FieldAggregationType> fieldAggregationTable;
private readonly List<TypeSetType> typeSets;
private readonly Dictionary<string, TypeSetType> typeSetTable;
private readonly List<Function> functions;
Expand Down Expand Up @@ -66,7 +66,7 @@ public int GlobalVariableSize
private set;
}

public int StructCount => structs.Count;
public int FieldAggregationCount => fieldAggregations.Count;

public int TypeSetCount => typeSets.Count;

Expand Down Expand Up @@ -99,8 +99,8 @@ internal CompilationUnity(Compiler compiler, string name, string fileName, bool
importTable = new();
globals = new();
globalTable = new();
structs = new();
structTable = new();
fieldAggregations = new();
fieldAggregationTable = new();
typeSets = new();
typeSetTable = new();
functions = new();
Expand Down Expand Up @@ -216,16 +216,16 @@ internal GlobalVariable DeclareGlobalVariable(string name, AbstractType type, So
return result;
}

public StructType FindStruct(string name, bool searchInImports = true)
public FieldAggregationType FindFieldAggregation(string name, bool searchInImports = true)
{
if (structTable.TryGetValue(name, out StructType result))
if (fieldAggregationTable.TryGetValue(name, out FieldAggregationType result))
return result;

if (searchInImports)
{
foreach (CompilationUnity unity in imports)
{
result = unity.FindStruct(name, false);
result = unity.FindFieldAggregation(name, false);
if (result != null)
return result;
}
Expand All @@ -234,9 +234,9 @@ public StructType FindStruct(string name, bool searchInImports = true)
return null;
}

public StructType GetStruct(int index)
public FieldAggregationType GeFieldAggregation(int index)
{
return structs[index];
return fieldAggregations[index];
}

internal StructType DeclareStruct(string name, SourceInterval interval)
Expand All @@ -246,8 +246,20 @@ internal StructType DeclareStruct(string name, SourceInterval interval)
return null;

StructType result = new(this, name, interval);
structs.Add(result);
structTable.Add(name, result);
fieldAggregations.Add(result);
fieldAggregationTable.Add(name, result);
return result;
}

internal ClassType DeclareClass(string name, SourceInterval interval)
{
NamedType nt = FindNamedType(name);
if (nt != null)
return null;

ClassType result = new(this, name, interval);
fieldAggregations.Add(result);
fieldAggregationTable.Add(name, result);
return result;
}

Expand Down Expand Up @@ -288,7 +300,7 @@ internal TypeSetType DeclareTypeSet(string name, AbstractType type, SourceInterv

public NamedType FindNamedType(string name)
{
StructType st = FindStruct(name);
FieldAggregationType st = FindFieldAggregation(name);
return st != null ? st : FindTypeSet(name);
}

Expand All @@ -310,13 +322,13 @@ public Function FindFunction(string name, bool searchInImports = true)
return null;
}

internal Function DeclareFunction(string name, SourceInterval interval, bool isExtern)
internal Function DeclareFunction(FieldAggregationType declaringType, string name, SourceInterval interval, bool isExtern)
{
Function result = FindFunction(name);
if (result != null)
return null;

result = new Function(this, name, interval, isExtern);
result = new Function(this, declaringType, name, interval, isExtern);
functions.Add(result);
functionTable.Add(name, result);
return result;
Expand Down Expand Up @@ -347,6 +359,7 @@ public UnresolvedType GetUndeclaredType(int index)
internal void Compile(Assembler assembler)
{
Compiler.unity = this;
Compiler.declaringType = null;

GlobalStartOffset = Compiler.globalVariableOffset;

Expand All @@ -373,12 +386,12 @@ internal void Resolve()
{
foreach (UnresolvedType type in undeclaredTypes)
{
StructType st = FindStruct(type.Name);
FieldAggregationType st = FindFieldAggregation(type.Name);

type.ReferencedType = st ?? throw new CompilerException(type.Interval, $"Tipo não declarado: '{type.Name}'.");
}

foreach (StructType st in structs)
foreach (FieldAggregationType st in fieldAggregations)
st.Resolve();

foreach (TypeSetType ts in typeSets)
Expand Down
5 changes: 4 additions & 1 deletion Comp/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static int GetAlignedSize(int sizeInBytes, int alignSize = sizeof(int))
private readonly Dictionary<string, int> externalFunctionMap;

internal CompilationUnity unity;
internal FieldAggregationType declaringType;
internal CompilationUnity unitySystem;
internal Function function;

Expand All @@ -54,13 +55,14 @@ public Compiler(string unityPath = null)
externalFunctionMap = new Dictionary<string, int>();

unity = null;
declaringType = null;
function = null;
}

public int AddExternalFunction(string name, int paramSize)
{
if (externalFunctionMap.ContainsKey(name))
throw new Exception($"Função externa '{name}' já adicionada.");
throw new Exception($"Função esterna '{name}' já adicionada.");

int index = externalFunctions.Count;
externalFunctions.Add((name, paramSize));
Expand Down Expand Up @@ -945,6 +947,7 @@ private void Initialize()
{
globalVariableOffset = sizeof(int);
function = null;
declaringType = null;
unity = null;
program = null;
lexer = null;
Expand Down
88 changes: 76 additions & 12 deletions Comp/CompilerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Comp.Lex;
using Comp.Types;

using SimpleCompiler.GUI;

namespace Comp;

public partial class Compiler
Expand Down Expand Up @@ -564,7 +562,7 @@ private AbstractType ParseType(bool allowVoid = false)
{
Identifier id = lexer.NextIdentifier();
string name = id.Name;
result = unity.FindStruct(name);
result = unity.FindFieldAggregation(name);
if (result == null)
result = unity.AddUndeclaredType(name, id.Interval);
}
Expand Down Expand Up @@ -637,7 +635,7 @@ private void ParseParamsDeclaration(Function function)
}
}

private void ParseFieldsDeclaration(StructType st)
private void ParseFieldsDeclaration(FieldAggregationType st)
{
while (true)
{
Expand Down Expand Up @@ -697,10 +695,8 @@ private Statement ParseStatement()
case "se":
{
lexer.NextSymbol("(");

Expression expression = ParseExpression();

end = lexer.NextSymbol(")");
lexer.NextSymbol(")");

Statement thenStatement = ParseStatement();
interval = interval.Merge(thenStatement.Interval);
Expand Down Expand Up @@ -898,11 +894,11 @@ private DeclarationStatement ParseVariableDeclaration(bool allowInitializer = tr

private void ParseFunctionDeclaration()
{
bool isExtern = lexer.NextKeyword("externa", false) != null;
bool isExtern = lexer.NextKeyword("esterna", false) != null;

Identifier id = lexer.NextIdentifier();
string name = id.Name;
Function f = unity.DeclareFunction(name, id.Interval, isExtern) ?? throw new CompilerException(id.Interval, $"Função '{name}' já declarada.");
Function f = unity.DeclareFunction(declaringType, name, id.Interval, isExtern) ?? throw new CompilerException(id.Interval, $"Função '{name}' já declarada.");

lexer.NextSymbol("(");
if (lexer.NextSymbol(")", false) == null)
Expand Down Expand Up @@ -937,7 +933,59 @@ private void ParseStructDeclaration()
{
Identifier id = lexer.NextIdentifier();
string name = id.Name;
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo nomeado '{name}' já declarado.");
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");

lexer.NextSymbol("{");
if (lexer.NextSymbol("}", false) == null)
ParseFieldsDeclaration(st);
}

private void ParseClassDeclaration()
{
// TODO : Implementar

Identifier id = lexer.NextIdentifier();
string name = id.Name;
ClassType st = unity.DeclareClass(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");

lexer.NextSymbol("{");
if (lexer.NextSymbol("}", false) == null)
ParseFieldsDeclaration(st);
}

private void ParseInterfaceDeclaration()
{
// TODO : Implementar

Identifier id = lexer.NextIdentifier();
string name = id.Name;
ClassType st = unity.DeclareClass(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");

lexer.NextSymbol("{");
if (lexer.NextSymbol("}", false) == null)
ParseFieldsDeclaration(st);
}

private void ParseEnumDeclaration()
{
// TODO : Implementar

Identifier id = lexer.NextIdentifier();
string name = id.Name;
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");

lexer.NextSymbol("{");
if (lexer.NextSymbol("}", false) == null)
ParseFieldsDeclaration(st);
}

private void ParseUnionDeclaration()
{
// TODO : Implementar

Identifier id = lexer.NextIdentifier();
string name = id.Name;
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");

lexer.NextSymbol("{");
if (lexer.NextSymbol("}", false) == null)
Expand Down Expand Up @@ -1004,6 +1052,22 @@ private bool ParseDeclaration(bool endsWithBraces = true)
case "estrutura":
ParseStructDeclaration();
return true;

case "classe":
ParseClassDeclaration();
return true;

case "interface":
ParseInterfaceDeclaration();
return true;

case "enum":
ParseEnumDeclaration();
return true;

case "união":
ParseUnionDeclaration();
return true;
}

lexer.PreviusToken();
Expand All @@ -1012,7 +1076,7 @@ private bool ParseDeclaration(bool endsWithBraces = true)
Symbol start = lexer.NextSymbol("{", false);
if (start != null)
{
Function f = unity.DeclareFunction("@main", start.Interval, false);
Function f = unity.DeclareFunction(null, "@main", start.Interval, false);
unity.EntryPoint = f ?? throw new CompilerException(start.Interval, "Ponto de entrada já declarado.");

f.CreateEntryLabel();
Expand Down Expand Up @@ -1151,4 +1215,4 @@ private void ParseCompilationUnity(CompilationUnity unity)
lexer = oldLexer;
this.unity = oldUnity;
}
}
}
10 changes: 5 additions & 5 deletions Comp/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Comp;

public class Field : Variable
public class Field : Variable, IMember
{
public StructType Container
public FieldAggregationType DeclaringType
{
get;
}

internal Field(StructType container, string name, AbstractType type, SourceInterval interval, int offset = -1) : base(name, type, interval, offset)
internal Field(FieldAggregationType container, string name, AbstractType type, SourceInterval interval, int offset = -1) : base(name, type, interval, offset)
{
Container = container;
DeclaringType = container;
}
}
}
10 changes: 8 additions & 2 deletions Comp/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Comp;

public class Function
public class Function : IMember
{
private readonly List<Parameter> parameters;
private AbstractType returnType;
Expand Down Expand Up @@ -88,9 +88,15 @@ public Label ReturnLabel
private set;
}

internal Function(CompilationUnity unity, string name, SourceInterval interval, bool isExtern = false)
public FieldAggregationType DeclaringType
{
get;
}

internal Function(CompilationUnity unity, FieldAggregationType declaringType, string name, SourceInterval interval, bool isExtern = false)
{
Unity = unity;
DeclaringType = declaringType;
Name = name;
Interval = interval;
IsExtern = isExtern;
Expand Down
11 changes: 11 additions & 0 deletions Comp/IMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Comp.Types;

namespace Comp;

public interface IMember
{
public FieldAggregationType DeclaringType
{
get;
}
}
Loading

0 comments on commit 27f3d11

Please sign in to comment.