Skip to content

Commit

Permalink
Fix class instance not storing the class value
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirdabic committed Nov 19, 2023
1 parent ac609d6 commit e8e9f96
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Blaze Compiler/Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private Statement ParseTopDef()
private Statement.TopClassDef ParseClass(TokenType visibility)
{
Token name = Consume(TokenType.IDENTIFIER, "Expected class name after 'class'");
string parentName = Match(TokenType.COLON) ? (string)Consume(TokenType.IDENTIFIER, "Expected base class name").Value : null
string parentName = Match(TokenType.COLON) ? (string)Consume(TokenType.IDENTIFIER, "Expected base class name").Value : null;

Consume(TokenType.OPEN_BRACE, "Expected class body");

Expand Down
2 changes: 1 addition & 1 deletion Blaze Core/Interpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private void Execute()

List<IValue> args = new List<IValue>();

var instance = new ClassInstanceValue();
var instance = new ClassInstanceValue(null);
//Stack.Push(instance);

for (int i = 0; i < oparg; ++i)
Expand Down
5 changes: 5 additions & 0 deletions Blaze Core/Interpreter/Types/ClassInstanceValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class ClassInstanceValue : IValue, IValueProperties
public ClassValue Type;
public ClassEnv Properties;

public ClassInstanceValue(ClassValue classType)
{
Type = classType;
}

public bool AsBoolean()
{
return true;
Expand Down
9 changes: 6 additions & 3 deletions Blaze Core/Interpreter/Types/ClassValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ public string AsString()

public void New(Interpreter interpreter, IValue instance, List<IValue> args)
{
// do some base class initialization probably

// do some base class initialization in the future
var env = new ClassEnv(Closure, instance);
((ClassInstanceValue)instance).Properties = env;

// Guaranteed to be a class instance value
var cls_instance = (ClassInstanceValue)instance;
cls_instance.Properties = env;
cls_instance.Type = this;

foreach (var member in InstanceMembers)
env.Members[member] = new ClassEnv.Variable(Interpreter.NullInstance);
Expand Down

0 comments on commit e8e9f96

Please sign in to comment.