Skip to content

Commit e8948aa

Browse files
committed
- Retrabalhado o layout principal do programa, adicionando mais seções de depuração como a visualização do conteúdo de variáveis durante a execução. As informações de baixo nível agora por padrão ficam ocultas.
- Adicionado menus. - Corrigido alguns bugs. - Realizada mais refatorações no código.
1 parent 3371b51 commit e8948aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+5138
-3529
lines changed

assembler/Assembler.cs renamed to Asm/Assembler.cs

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
using System.Collections.Generic;
33
using System.IO;
44

5-
using vm;
5+
using Comp;
66

7-
namespace assembler;
7+
using VM;
8+
9+
namespace Asm;
810

911
public class Assembler
1012
{
@@ -17,6 +19,9 @@ public class Assembler
1719
private readonly List<(string, int)> externalFunctions;
1820
private readonly List<(string, int, int)> sourceCodeLines;
1921
private readonly Dictionary<(string, int), int> sourceCodeLineMap;
22+
private readonly List<GlobalVariable> globalVariables;
23+
private readonly List<LocalVariable> localVariables;
24+
private readonly List<Function> functions;
2025

2126
public long Position
2227
{
@@ -34,6 +39,12 @@ public long Position
3439

3540
public IEnumerable<(string, int, int)> SourceCodeLines => sourceCodeLines;
3641

42+
public IEnumerable<GlobalVariable> GlobalVariables => globalVariables;
43+
44+
public IEnumerable<LocalVariable> LocalVariables => localVariables;
45+
46+
public IEnumerable<Function> Functions => functions;
47+
3748
public Assembler()
3849
{
3950
output = new MemoryStream();
@@ -45,6 +56,9 @@ public Assembler()
4556
externalFunctions = new List<(string, int)>();
4657
sourceCodeLines = new List<(string, int, int)>();
4758
sourceCodeLineMap = new Dictionary<(string, int), int>();
59+
globalVariables = new List<GlobalVariable>();
60+
localVariables = new List<LocalVariable>();
61+
functions = new List<Function>();
4862
}
4963

5064
public void AddExternalFunctionNames((string, int)[] entries)
@@ -59,7 +73,8 @@ public void AddExternalFunctionNames((string, int)[] entries)
5973

6074
public int AddLine(string fileName, int line)
6175
{
62-
if (sourceCodeLineMap.TryGetValue((fileName, line), out int ip))
76+
int ip;
77+
if (sourceCodeLineMap.TryGetValue((fileName, line), out _))
6378
{
6479
ip = (int) Position;
6580
sourceCodeLineMap[(fileName, line)] = ip;
@@ -83,22 +98,61 @@ public void ClearLines()
8398
sourceCodeLineMap.Clear();
8499
}
85100

101+
public void AddGlobalVariable(GlobalVariable variable)
102+
{
103+
globalVariables.Add(variable);
104+
}
105+
106+
public void AddLocalVariable(LocalVariable variable)
107+
{
108+
localVariables.Add(variable);
109+
}
110+
111+
public void AddFunction(Function function)
112+
{
113+
functions.Add(function);
114+
}
115+
116+
public void ClearVariables()
117+
{
118+
globalVariables.Clear();
119+
localVariables.Clear();
120+
}
121+
122+
public void ClearGlobalVariables()
123+
{
124+
globalVariables.Clear();
125+
}
126+
127+
public void ClearLocalVariables()
128+
{
129+
localVariables.Clear();
130+
}
131+
132+
public void ClearFunctions()
133+
{
134+
functions.Clear();
135+
}
136+
86137
public void CopyCode(byte[] output)
87138
{
88139
CopyCode(output, 0, output.Length);
89140
}
90141

91142
public void CopyCode(byte[] output, int off, int len)
92143
{
93-
long count = this.output.Position;
144+
var position = this.output.Position;
94145
this.output.Position = 0;
95146
this.output.Read(output, off, len);
96-
this.output.Position = count;
147+
this.output.Position = position;
97148
}
98149

99150
public void CopyCode(Stream output)
100151
{
152+
var position = this.output.Position;
153+
this.output.Position = 0;
101154
this.output.WriteTo(output);
155+
this.output.Position = position;
102156
}
103157

104158
public void CopyConstantBuffer(byte[] output)
@@ -108,10 +162,10 @@ public void CopyConstantBuffer(byte[] output)
108162

109163
public void CopyConstantBuffer(byte[] output, int off, int len)
110164
{
111-
long count = constantOut.Position;
165+
var position = constantOut.Position;
112166
constantOut.Position = 0;
113167
constantOut.Read(output, off, len);
114-
constantOut.Position = count;
168+
constantOut.Position = position;
115169
}
116170

117171
public void CopyConstantBuffer(Stream output)
@@ -122,6 +176,7 @@ public void CopyConstantBuffer(Stream output)
122176
public void ReserveConstantBuffer(int size)
123177
{
124178
constantOut.Position = 0;
179+
constantOut.SetLength(0);
125180
constantOut.Seek(size, SeekOrigin.Current);
126181
}
127182

@@ -197,13 +252,19 @@ public void WriteConstant(int offset, byte[] value, int index, int count)
197252
public void Clear()
198253
{
199254
output.Position = 0;
255+
output.SetLength(0);
256+
257+
constantOut.SetLength(0);
200258
constantOut.Position = 0;
201259

202260
issuedLabels.Clear();
203261
bindedLabels.Clear();
204262
externalFunctions.Clear();
205263
sourceCodeLines.Clear();
206264
sourceCodeLineMap.Clear();
265+
globalVariables.Clear();
266+
localVariables.Clear();
267+
functions.Clear();
207268
}
208269

209270
public void EmitData(byte value)
@@ -305,6 +366,15 @@ public void Emit(Assembler other)
305366
label.bindedIP += (int) startPosition;
306367
bindedLabels.Add(label);
307368
}
369+
370+
foreach (var global in other.globalVariables)
371+
globalVariables.Add(global);
372+
373+
foreach (var local in other.localVariables)
374+
localVariables.Add(local);
375+
376+
foreach (var function in other.functions)
377+
functions.Add(function);
308378
}
309379

310380
public void EmitNop()

assembler/Label.cs renamed to Asm/Label.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace assembler;
3+
namespace Asm;
44

55
public class Label
66
{

compiler/CompilationUnity.cs renamed to Comp/CompilationUnity.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Collections.Generic;
22

3-
using assembler;
3+
using Asm;
44

5-
using compiler.types;
5+
using Comp.Types;
66

7-
namespace compiler;
7+
namespace Comp;
88

99
public class CompilationUnity
1010
{
@@ -350,9 +350,13 @@ internal void Compile(Assembler assembler)
350350

351351
GlobalStartOffset = Compiler.globalVariableOffset;
352352

353-
foreach (Function f in functions)
353+
foreach (var global in globals)
354+
assembler.AddGlobalVariable(global);
355+
356+
foreach (var function in functions)
354357
{
355-
Compiler.function = f;
358+
assembler.AddFunction(function);
359+
Compiler.function = function;
356360
Compiler.CompileFunction(assembler);
357361
}
358362

compiler/Compiler.cs renamed to Comp/Compiler.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
using System.Collections.Generic;
33
using System.IO;
44

5-
using assembler;
5+
using Asm;
66

7-
using compiler.lexer;
8-
using compiler.types;
7+
using Comp.Lex;
8+
using Comp.Types;
99

10-
namespace compiler;
10+
namespace Comp;
1111

1212
public partial class Compiler
1313
{
@@ -135,8 +135,8 @@ private void CompileCast(Context context, Assembler assembler, Assembler beforeC
135135
if (!isExplicit && tp.Primitive != Primitive.BOOL)
136136
throw new CompilerException(interval, $"O tipo '{fromType}' não pode ser convertido implicitamente para o tipo '{toType}'.");
137137

138-
// TODO Implementar
139-
throw new CompilerException(interval, $"Operação não implementada para o tipo '{p}'.");
138+
CompileInt32Conversion(assembler, tp);
139+
break;
140140

141141
case Primitive.BYTE:
142142
if (isExplicit ? tp.Primitive == Primitive.BOOL : tp.Primitive is < Primitive.BYTE or Primitive.CHAR)
@@ -423,21 +423,6 @@ private void CompileArrayIndexer(Context context, Assembler assembler, Expressio
423423
}
424424
}
425425

426-
#pragma warning disable IDE0051 // Remover membros privados não utilizados
427-
private Variable CheckVariable(Context context, string name, SourceInterval interval)
428-
#pragma warning restore IDE0051 // Remover membros privados não utilizados
429-
{
430-
Variable var = context.FindVariable(name);
431-
if (var == null)
432-
{
433-
var = unity.FindGlobalVariable(name);
434-
if (var == null)
435-
throw new CompilerException(interval, $"Variável '{name}' não declarada.");
436-
}
437-
438-
return var;
439-
}
440-
441426
private void CompileStatement(Context context, Assembler assembler, Statement statement)
442427
{
443428
assembler.AddLine(statement.Interval.FileName, statement.Interval.FirstLine);
@@ -474,8 +459,8 @@ private void CompileStatement(Context context, Assembler assembler, Statement st
474459
Assembler beforeCastAssembler = new();
475460
Assembler castAssembler = new();
476461
Assembler storeAssembler = new();
477-
Variable castTempVar = null;
478-
if (function.ReturnType is PrimitiveType or PointerType)
462+
Variable castTempVar;
463+
if (function.ReturnType is PrimitiveType or PointerType or StringType)
479464
{
480465
AbstractType returnType = CompileExpression(context, castAssembler, expr, out Variable tempVar);
481466
CompileCast(context, castAssembler, beforeCastAssembler, returnType, function.ReturnType, false, expr.Interval, out castTempVar);
@@ -807,10 +792,14 @@ private void CompileLocalVariableDeclaration(Context context, Assembler assemble
807792
{
808793
statement.Resolve();
809794
AbstractType type = statement.Type;
795+
810796
foreach (var (name, initializer) in statement)
811797
{
812798
Variable var = context.DeclareVariable(name, type, statement.Interval) ?? throw new CompilerException(statement.Interval, $"Variável local '{name}' já declarada.");
813799

800+
if (assembler != null && var is LocalVariable local)
801+
assembler.AddLocalVariable(local);
802+
814803
if (initializer != null)
815804
{
816805
Assembler beforeStoreAssembler = new();
@@ -822,7 +811,7 @@ private void CompileLocalVariableDeclaration(Context context, Assembler assemble
822811
}
823812
else
824813
{
825-
useVar = var.Type is PrimitiveType or PointerType;
814+
useVar = var.Type is PrimitiveType or PointerType or StringType;
826815
if (!useVar)
827816
beforeStoreAssembler.EmitLoadLocalResidentAddress(var.Offset);
828817
}

compiler/CompilerException.cs renamed to Comp/CompilerException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace compiler;
3+
namespace Comp;
44

55
public class CompilerException : Exception
66
{

compiler/CompilerLoad.cs renamed to Comp/CompilerLoad.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using assembler;
1+
using Asm;
22

3-
using compiler.types;
3+
using Comp.Types;
44

5-
namespace compiler;
5+
namespace Comp;
66

77
public partial class Compiler
88
{

0 commit comments

Comments
 (0)