Skip to content

Commit

Permalink
feat: blocks create scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Dec 17, 2024
1 parent 473dde5 commit 494df6b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
9 changes: 7 additions & 2 deletions Heir/BytecodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ public List<Instruction> VisitSyntaxTree(SyntaxTree syntaxTree) =>
.ToList();

// TODO: create scope
public List<Instruction> VisitBlock(Block block) => GenerateStatementsBytecode(block.Statements);
public List<Instruction> VisitBlock(Block block) =>
GenerateStatementsBytecode(block.Statements)
.Prepend(new Instruction(block, OpCode.BEGINSCOPE))
.Append(new Instruction(block, OpCode.ENDSCOPE))
.ToList();

public List<Instruction> VisitVariableDeclaration(VariableDeclaration variableDeclaration) =>
new List<Instruction>([new Instruction(variableDeclaration.Name, OpCode.PUSH, variableDeclaration.Name.Token.Text)])
.Concat(variableDeclaration.Initializer != null ? GenerateBytecode(variableDeclaration.Initializer) : [])
.Append(new Instruction(variableDeclaration, OpCode.STORE))
.Append(new Instruction(variableDeclaration, OpCode.STORE, false))
.ToList();

public List<Instruction> VisitExpressionStatement(ExpressionStatement expressionStatement) => GenerateBytecode(expressionStatement.Expression);
Expand Down
3 changes: 3 additions & 0 deletions Heir/CodeGeneration/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{
public enum OpCode : byte
{
BEGINSCOPE,
ENDSCOPE,

PUSH,
PUSHNONE,
POP,
Expand Down
21 changes: 18 additions & 3 deletions Heir/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ public sealed class VirtualMachine
{
public DiagnosticBag Diagnostics { get; }
public Scope GlobalScope { get; }
public Scope Scope { get; }
public Scope Scope { get; private set; }

private readonly Stack<StackFrame> _stack = new();
private readonly Stack<StackFrame> _stack = [];
private readonly Bytecode _bytecode;
private Scope _enclosingScope;
private int _pointer = 0;

public VirtualMachine(Bytecode bytecode, Scope? scope = null)
{
Diagnostics = bytecode.Diagnostics;
GlobalScope = new();
Scope = scope ?? GlobalScope;
_enclosingScope = Scope;
_bytecode = bytecode;
}

Expand Down Expand Up @@ -62,6 +64,17 @@ public VirtualMachine(Bytecode bytecode, Scope? scope = null)
case OpCode.RETURN:
return _stack.Pop();

case OpCode.BEGINSCOPE:
_enclosingScope = Scope;
Scope = new(_enclosingScope);
Advance();
break;
case OpCode.ENDSCOPE:
Scope = _enclosingScope;
_enclosingScope = Scope.Enclosing ?? Scope;
Advance();
break;

case OpCode.PUSH:
case OpCode.PUSHNONE:
_stack.Push(CreateStackFrameFromInstruction());
Expand Down Expand Up @@ -123,7 +136,9 @@ public VirtualMachine(Bytecode bytecode, Scope? scope = null)
else
Scope.Define(name, initializer.Value);

_stack.Push(initializer);
if (instruction.Operand as bool? ?? true)
_stack.Push(initializer);

Advance();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Test.heir
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1 + 2
let x = 1;

0 comments on commit 494df6b

Please sign in to comment.