Skip to content

Commit c9025cd

Browse files
committed
BugFixes
1 parent 1787425 commit c9025cd

File tree

11 files changed

+154
-20
lines changed

11 files changed

+154
-20
lines changed

src/Yabal.Compiler/InstructionBuildResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public string ToAssembly(bool addComments = false)
114114

115115
public int[] ToArray()
116116
{
117-
var array = new int[_length];
117+
var array = new int[0xFFFF];
118118
var i = 0;
119119

120120
foreach (var value in GetBytes())

src/Yabal.Compiler/Yabal/Ast/Statement/ExpressionStatement.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,29 @@ public override void Initialize(YabalBuilder builder)
99

1010
public override void Build(YabalBuilder builder)
1111
{
12-
Expression.BuildExpression(builder, true, null);
12+
if (builder.Debug && Expression is CallExpression { Type.StaticType: not (StaticType.Void or StaticType.Unknown) } callExpression)
13+
{
14+
var temp = builder.GetTemporaryVariable(global: true, size: callExpression.Type.Size);
15+
16+
callExpression.BuildExpressionToPointer(builder, callExpression.Type, temp);
1317

14-
if (Expression is AddressExpression address and (IdentifierExpression or MemberExpression))
18+
builder.AddVariableDebug(Range, callExpression.Type, temp);
19+
}
20+
else
1521
{
16-
address.ShowDebug(builder);
22+
Expression.BuildExpression(builder, true, null);
23+
24+
if (Expression is AddressExpression address and (IdentifierExpression or MemberExpression or ArrayAccessExpression))
25+
{
26+
address.ShowDebug(builder);
27+
}
1728
}
1829
}
1930

31+
public bool ShowDebug => Expression is
32+
(AddressExpression and (IdentifierExpression or MemberExpression or ArrayAccessExpression)) or
33+
(CallExpression { Type.StaticType: not StaticType.Void });
34+
2035
public override Statement CloneStatement()
2136
{
2237
return new ExpressionStatement(Range, Expression.CloneExpression());
@@ -26,7 +41,7 @@ public override Statement Optimize()
2641
{
2742
return new ExpressionStatement(Range, Expression switch
2843
{
29-
IdentifierExpression or MemberExpression => Expression,
44+
IdentifierExpression or MemberExpression or ArrayAccessExpression or CallExpression => Expression,
3045
_ => Expression.Optimize(null)
3146
});
3247
}

src/Yabal.Compiler/Yabal/Ast/Statement/ReturnStatement.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public override void Initialize(YabalBuilder builder)
1010
{
1111
Expression?.Initialize(builder);
1212
ReturnType = builder.ReturnType;
13+
14+
if (ReturnType != null && Expression is ITypeExpression typeExpression)
15+
{
16+
typeExpression.Initialize(builder, ReturnType);
17+
}
1318
}
1419

1520
public override void Build(YabalBuilder builder)

src/Yabal.Compiler/Yabal/Visitor/YabalVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ public override Node VisitOrElseExpression(YabalParser.OrElseExpressionContext c
736736

737737
public override Node VisitShiftExpression(YabalParser.ShiftExpressionContext context)
738738
{
739-
var @operator = context.ShiftLeft() != null ? BinaryOperator.LeftShift : BinaryOperator.RightShift;
739+
var @operator = context.Less()?.Length > 0 ? BinaryOperator.LeftShift : BinaryOperator.RightShift;
740740
return CreateBinary(context, context.expression(), @operator);
741741
}
742742

src/Yabal.Compiler/Yabal/YabalBuilder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public YabalBuilder(YabalContext context)
8989
_errors = new Dictionary<SourceRange, List<CompileError>>();
9090
BinaryOperators = new Dictionary<(BinaryOperator, LanguageType, LanguageType), Function>();
9191
CastOperators = new Dictionary<(LanguageType, LanguageType), Function>();
92+
ArrowFunctions = new List<Function>();
9293

9394
_visitor = new TypeVisitor();
9495
_globalBlock = new BlockStack { IsGlobal = true };
@@ -120,6 +121,7 @@ public YabalBuilder(YabalBuilder parent)
120121
_errors = parent._errors;
121122
BinaryOperators = parent.BinaryOperators;
122123
CastOperators = parent.CastOperators;
124+
ArrowFunctions = parent.ArrowFunctions;
123125
Options = parent.Options;
124126

125127
Debug = parent.Debug;
@@ -129,9 +131,9 @@ public YabalBuilder(YabalBuilder parent)
129131

130132
public Dictionary<(BinaryOperator, LanguageType, LanguageType), Function> BinaryOperators { get; }
131133

132-
public Dictionary<(LanguageType, LanguageType), Function> CastOperators { get; } = new();
134+
public Dictionary<(LanguageType, LanguageType), Function> CastOperators { get; }
133135

134-
public List<Function> ArrowFunctions { get; } = new();
136+
public List<Function> ArrowFunctions { get; }
135137

136138
public InstructionPointer StackAllocPointer => _stackAllocPointer;
137139

@@ -830,7 +832,7 @@ public InstructionBuildResult Build(int offset = 0)
830832

831833
AddHeader(builder);
832834
builder.AddRange(_builder);
833-
builder.Jump(0xFFFF);
835+
builder.Jump(0xFFFE);
834836
AddStrings(builder);
835837

836838
return builder.Build(offset);

src/Yabal.Compiler/Yabal/YabalLexer.g4

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ Xor : '^';
2929
And : '&';
3030
Mod : '%';
3131
Not : '~';
32-
ShiftLeft : '<<';
33-
ShiftRight : '>>';
3432
Add : '+';
3533
Sub : '-';
3634
Mul : '*';

src/Yabal.Compiler/Yabal/YabalParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ expression
5353
| expression DivEqual expression # DivEqualExpression
5454
| expression (Div|Mul|Mod) expression # DivMulModBinaryExpression
5555
| expression (Add|Sub) expression # PlusSubBinaryExpression
56-
| expression (ShiftLeft|ShiftRight) expression # ShiftExpression
56+
| expression (Less Less|Greater Greater) expression # ShiftExpression
5757
| expression (GreaterEqual|Greater|LessEqual|Less) expression # ComparisonExpression
5858
| expression (Equals|NotEquals) expression # EqualExpression
5959
| expression And expression # AndExpression

src/Yabal.Emulator/Devices/Cpu.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ public void Halt()
126126
var index = context.ProgramCounter;
127127
context.MemoryIndex = index;
128128

129-
if (index >= 0x3FFE)
129+
if (index >= 0xFFFF)
130130
{
131+
_handler.Halt();
131132
_halt = true;
132133
break;
133134
}
@@ -236,7 +237,7 @@ public void Load(Stream stream)
236237
private unsafe ref struct StepContext
237238
{
238239
private CpuMemory<THandler> _memory;
239-
private Span<int> _memoryPointer;
240+
private int* _memoryPointer;
240241
private readonly CpuMemory<THandler>[] _banks;
241242
private readonly InstructionReference* _instructionPointer;
242243

@@ -262,7 +263,7 @@ public StepContext(
262263
{
263264
_banks = banks;
264265
_memory = banks[context.Bank];
265-
_memoryPointer = _memory.Data.AsSpan();
266+
_memoryPointer = (int*) Unsafe.AsPointer(ref _memory.Data[0]);
266267
_instructionPointer = instructionPointer;
267268
Handler = handler;
268269
A = context.A;
@@ -283,7 +284,7 @@ public void SetBank(int id)
283284
{
284285
Bank = id;
285286
_memory = _banks[id];
286-
_memoryPointer = _memory.Data.AsSpan();
287+
_memoryPointer = (int*) Unsafe.AsPointer(ref _memory.Data[0]);
287288
}
288289
}
289290

src/Yabal.Wasm/Interop.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static unsafe void Compile(byte* bytes, int byteLength)
5555
}
5656
catch (Exception e)
5757
{
58-
Console.WriteLine("Failed to compile: " + e.Message);
58+
Console.WriteLine("Failed to compile: " + e);
5959
}
6060
}
6161

@@ -72,8 +72,9 @@ public static int Step(int amount)
7272
cpu.Step(amount);
7373
return cpu.ProgramCounter;
7474
}
75-
catch (Exception)
75+
catch (Exception e)
7676
{
77+
Console.WriteLine("Failed to step: " + e);
7778
cpu.Halt();
7879
return -1;
7980
}

src/Yabal.Wasm/WasmHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public override unsafe void FlushScreen()
3030

3131
public override void Halt()
3232
{
33-
Interop.Halt();
3433
FlushScreen();
34+
Interop.Halt();
3535
}
3636

3737
public override unsafe void ShowVariable(int line, int offset, Span<int> value)
3838
{
3939
fixed (int* screen = value)
4040
{
41-
Interop.ShowVariable(line, offset, value.Length, (int*)screen);
41+
Interop.ShowVariable(line, offset, value.Length, screen);
4242
}
4343
}
4444
}

0 commit comments

Comments
 (0)