Skip to content

Commit 27f3d11

Browse files
committed
- Iniciada a adição de suporte à arrays dinâmicos, estes também serão 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.
1 parent ec9001e commit 27f3d11

27 files changed

+576
-311
lines changed

Comp/CompilationUnity.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public enum ImportResult
2121
private readonly Dictionary<string, CompilationUnity> importTable;
2222
private readonly List<GlobalVariable> globals;
2323
private readonly Dictionary<string, GlobalVariable> globalTable;
24-
private readonly List<StructType> structs;
25-
private readonly Dictionary<string, StructType> structTable;
24+
private readonly List<FieldAggregationType> fieldAggregations;
25+
private readonly Dictionary<string, FieldAggregationType> fieldAggregationTable;
2626
private readonly List<TypeSetType> typeSets;
2727
private readonly Dictionary<string, TypeSetType> typeSetTable;
2828
private readonly List<Function> functions;
@@ -66,7 +66,7 @@ public int GlobalVariableSize
6666
private set;
6767
}
6868

69-
public int StructCount => structs.Count;
69+
public int FieldAggregationCount => fieldAggregations.Count;
7070

7171
public int TypeSetCount => typeSets.Count;
7272

@@ -99,8 +99,8 @@ internal CompilationUnity(Compiler compiler, string name, string fileName, bool
9999
importTable = new();
100100
globals = new();
101101
globalTable = new();
102-
structs = new();
103-
structTable = new();
102+
fieldAggregations = new();
103+
fieldAggregationTable = new();
104104
typeSets = new();
105105
typeSetTable = new();
106106
functions = new();
@@ -216,16 +216,16 @@ internal GlobalVariable DeclareGlobalVariable(string name, AbstractType type, So
216216
return result;
217217
}
218218

219-
public StructType FindStruct(string name, bool searchInImports = true)
219+
public FieldAggregationType FindFieldAggregation(string name, bool searchInImports = true)
220220
{
221-
if (structTable.TryGetValue(name, out StructType result))
221+
if (fieldAggregationTable.TryGetValue(name, out FieldAggregationType result))
222222
return result;
223223

224224
if (searchInImports)
225225
{
226226
foreach (CompilationUnity unity in imports)
227227
{
228-
result = unity.FindStruct(name, false);
228+
result = unity.FindFieldAggregation(name, false);
229229
if (result != null)
230230
return result;
231231
}
@@ -234,9 +234,9 @@ public StructType FindStruct(string name, bool searchInImports = true)
234234
return null;
235235
}
236236

237-
public StructType GetStruct(int index)
237+
public FieldAggregationType GeFieldAggregation(int index)
238238
{
239-
return structs[index];
239+
return fieldAggregations[index];
240240
}
241241

242242
internal StructType DeclareStruct(string name, SourceInterval interval)
@@ -246,8 +246,20 @@ internal StructType DeclareStruct(string name, SourceInterval interval)
246246
return null;
247247

248248
StructType result = new(this, name, interval);
249-
structs.Add(result);
250-
structTable.Add(name, result);
249+
fieldAggregations.Add(result);
250+
fieldAggregationTable.Add(name, result);
251+
return result;
252+
}
253+
254+
internal ClassType DeclareClass(string name, SourceInterval interval)
255+
{
256+
NamedType nt = FindNamedType(name);
257+
if (nt != null)
258+
return null;
259+
260+
ClassType result = new(this, name, interval);
261+
fieldAggregations.Add(result);
262+
fieldAggregationTable.Add(name, result);
251263
return result;
252264
}
253265

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

289301
public NamedType FindNamedType(string name)
290302
{
291-
StructType st = FindStruct(name);
303+
FieldAggregationType st = FindFieldAggregation(name);
292304
return st != null ? st : FindTypeSet(name);
293305
}
294306

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

313-
internal Function DeclareFunction(string name, SourceInterval interval, bool isExtern)
325+
internal Function DeclareFunction(FieldAggregationType declaringType, string name, SourceInterval interval, bool isExtern)
314326
{
315327
Function result = FindFunction(name);
316328
if (result != null)
317329
return null;
318330

319-
result = new Function(this, name, interval, isExtern);
331+
result = new Function(this, declaringType, name, interval, isExtern);
320332
functions.Add(result);
321333
functionTable.Add(name, result);
322334
return result;
@@ -347,6 +359,7 @@ public UnresolvedType GetUndeclaredType(int index)
347359
internal void Compile(Assembler assembler)
348360
{
349361
Compiler.unity = this;
362+
Compiler.declaringType = null;
350363

351364
GlobalStartOffset = Compiler.globalVariableOffset;
352365

@@ -373,12 +386,12 @@ internal void Resolve()
373386
{
374387
foreach (UnresolvedType type in undeclaredTypes)
375388
{
376-
StructType st = FindStruct(type.Name);
389+
FieldAggregationType st = FindFieldAggregation(type.Name);
377390

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

381-
foreach (StructType st in structs)
394+
foreach (FieldAggregationType st in fieldAggregations)
382395
st.Resolve();
383396

384397
foreach (TypeSetType ts in typeSets)

Comp/Compiler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static int GetAlignedSize(int sizeInBytes, int alignSize = sizeof(int))
2828
private readonly Dictionary<string, int> externalFunctionMap;
2929

3030
internal CompilationUnity unity;
31+
internal FieldAggregationType declaringType;
3132
internal CompilationUnity unitySystem;
3233
internal Function function;
3334

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

5657
unity = null;
58+
declaringType = null;
5759
function = null;
5860
}
5961

6062
public int AddExternalFunction(string name, int paramSize)
6163
{
6264
if (externalFunctionMap.ContainsKey(name))
63-
throw new Exception($"Função externa '{name}' já adicionada.");
65+
throw new Exception($"Função esterna '{name}' já adicionada.");
6466

6567
int index = externalFunctions.Count;
6668
externalFunctions.Add((name, paramSize));
@@ -945,6 +947,7 @@ private void Initialize()
945947
{
946948
globalVariableOffset = sizeof(int);
947949
function = null;
950+
declaringType = null;
948951
unity = null;
949952
program = null;
950953
lexer = null;

Comp/CompilerParser.cs

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using Comp.Lex;
44
using Comp.Types;
55

6-
using SimpleCompiler.GUI;
7-
86
namespace Comp;
97

108
public partial class Compiler
@@ -564,7 +562,7 @@ private AbstractType ParseType(bool allowVoid = false)
564562
{
565563
Identifier id = lexer.NextIdentifier();
566564
string name = id.Name;
567-
result = unity.FindStruct(name);
565+
result = unity.FindFieldAggregation(name);
568566
if (result == null)
569567
result = unity.AddUndeclaredType(name, id.Interval);
570568
}
@@ -637,7 +635,7 @@ private void ParseParamsDeclaration(Function function)
637635
}
638636
}
639637

640-
private void ParseFieldsDeclaration(StructType st)
638+
private void ParseFieldsDeclaration(FieldAggregationType st)
641639
{
642640
while (true)
643641
{
@@ -697,10 +695,8 @@ private Statement ParseStatement()
697695
case "se":
698696
{
699697
lexer.NextSymbol("(");
700-
701698
Expression expression = ParseExpression();
702-
703-
end = lexer.NextSymbol(")");
699+
lexer.NextSymbol(")");
704700

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

899895
private void ParseFunctionDeclaration()
900896
{
901-
bool isExtern = lexer.NextKeyword("externa", false) != null;
897+
bool isExtern = lexer.NextKeyword("esterna", false) != null;
902898

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

907903
lexer.NextSymbol("(");
908904
if (lexer.NextSymbol(")", false) == null)
@@ -937,7 +933,59 @@ private void ParseStructDeclaration()
937933
{
938934
Identifier id = lexer.NextIdentifier();
939935
string name = id.Name;
940-
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo nomeado '{name}' já declarado.");
936+
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");
937+
938+
lexer.NextSymbol("{");
939+
if (lexer.NextSymbol("}", false) == null)
940+
ParseFieldsDeclaration(st);
941+
}
942+
943+
private void ParseClassDeclaration()
944+
{
945+
// TODO : Implementar
946+
947+
Identifier id = lexer.NextIdentifier();
948+
string name = id.Name;
949+
ClassType st = unity.DeclareClass(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");
950+
951+
lexer.NextSymbol("{");
952+
if (lexer.NextSymbol("}", false) == null)
953+
ParseFieldsDeclaration(st);
954+
}
955+
956+
private void ParseInterfaceDeclaration()
957+
{
958+
// TODO : Implementar
959+
960+
Identifier id = lexer.NextIdentifier();
961+
string name = id.Name;
962+
ClassType st = unity.DeclareClass(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");
963+
964+
lexer.NextSymbol("{");
965+
if (lexer.NextSymbol("}", false) == null)
966+
ParseFieldsDeclaration(st);
967+
}
968+
969+
private void ParseEnumDeclaration()
970+
{
971+
// TODO : Implementar
972+
973+
Identifier id = lexer.NextIdentifier();
974+
string name = id.Name;
975+
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");
976+
977+
lexer.NextSymbol("{");
978+
if (lexer.NextSymbol("}", false) == null)
979+
ParseFieldsDeclaration(st);
980+
}
981+
982+
private void ParseUnionDeclaration()
983+
{
984+
// TODO : Implementar
985+
986+
Identifier id = lexer.NextIdentifier();
987+
string name = id.Name;
988+
StructType st = unity.DeclareStruct(name, id.Interval) ?? throw new CompilerException(id.Interval, $"Tipo '{name}' já declarado.");
941989

942990
lexer.NextSymbol("{");
943991
if (lexer.NextSymbol("}", false) == null)
@@ -1004,6 +1052,22 @@ private bool ParseDeclaration(bool endsWithBraces = true)
10041052
case "estrutura":
10051053
ParseStructDeclaration();
10061054
return true;
1055+
1056+
case "classe":
1057+
ParseClassDeclaration();
1058+
return true;
1059+
1060+
case "interface":
1061+
ParseInterfaceDeclaration();
1062+
return true;
1063+
1064+
case "enum":
1065+
ParseEnumDeclaration();
1066+
return true;
1067+
1068+
case "união":
1069+
ParseUnionDeclaration();
1070+
return true;
10071071
}
10081072

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

10181082
f.CreateEntryLabel();
@@ -1151,4 +1215,4 @@ private void ParseCompilationUnity(CompilationUnity unity)
11511215
lexer = oldLexer;
11521216
this.unity = oldUnity;
11531217
}
1154-
}
1218+
}

Comp/Field.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Comp;
44

5-
public class Field : Variable
5+
public class Field : Variable, IMember
66
{
7-
public StructType Container
7+
public FieldAggregationType DeclaringType
88
{
99
get;
1010
}
1111

12-
internal Field(StructType container, string name, AbstractType type, SourceInterval interval, int offset = -1) : base(name, type, interval, offset)
12+
internal Field(FieldAggregationType container, string name, AbstractType type, SourceInterval interval, int offset = -1) : base(name, type, interval, offset)
1313
{
14-
Container = container;
14+
DeclaringType = container;
1515
}
16-
}
16+
}

Comp/Function.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Comp;
99

10-
public class Function
10+
public class Function : IMember
1111
{
1212
private readonly List<Parameter> parameters;
1313
private AbstractType returnType;
@@ -88,9 +88,15 @@ public Label ReturnLabel
8888
private set;
8989
}
9090

91-
internal Function(CompilationUnity unity, string name, SourceInterval interval, bool isExtern = false)
91+
public FieldAggregationType DeclaringType
92+
{
93+
get;
94+
}
95+
96+
internal Function(CompilationUnity unity, FieldAggregationType declaringType, string name, SourceInterval interval, bool isExtern = false)
9297
{
9398
Unity = unity;
99+
DeclaringType = declaringType;
94100
Name = name;
95101
Interval = interval;
96102
IsExtern = isExtern;

Comp/IMember.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Comp.Types;
2+
3+
namespace Comp;
4+
5+
public interface IMember
6+
{
7+
public FieldAggregationType DeclaringType
8+
{
9+
get;
10+
}
11+
}

0 commit comments

Comments
 (0)