Skip to content

Commit

Permalink
Merge pull request #224 from stevehalliwell/frozen-init
Browse files Browse the repository at this point in the history
Merge frozen-init into main: Simplify MakeInstance vm logic
  • Loading branch information
stevehalliwell authored Dec 14, 2023
2 parents 9913576 + 68b38b7 commit cb62014
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 238 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ var
var anAddress = Address();
anAddress.number = 7;

//classes are the most feature rich.
//classes are the feature rich.
// The order of elements declared within the class is enforced
class MyFoo
{
Expand Down
19 changes: 9 additions & 10 deletions ulox/ulox.core.bench/BenchmarkScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ fun RandVec2() (x,y)
class Ball
{
init()
{
var (x,y) = RandVec2();
this.posx = x;
this.posy = y;
var posx = 0;
var posy = 0;
var velx = 0;
var vely = 0;
var (vx,vy) = RandVec2();
this.velx = vx;
this.vely = vy;
init(posx, posy, velx, vely)
{
}
}
Expand All @@ -93,7 +91,9 @@ fun SetupGame()
for(var i = 0; i < numBallsToSpawn; i += 1)
{
balls.Add(Ball());
var (x,y) = RandVec2();
var (vx,vy) = RandVec2();
balls.Add(Ball(x,y,vx,vy));
}
print(balls.Count());
}
Expand All @@ -106,7 +106,6 @@ fun Update()
loop balls
{
//print(GenerateStackDump());
item.posx = item.posx + item.velx * dt;
item.posy = item.posy + item.vely * dt;
Expand Down
47 changes: 20 additions & 27 deletions ulox/ulox.core.tests/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,8 @@ public void Engine_Class_BoundMethod()
{
testEngine.Run(@"
class CoffeeMaker {
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
brew() {
print (""Enjoy your cup of "" + this.coffee);
Expand All @@ -300,9 +299,8 @@ public void Engine_Class_BoundMethod_InternalAndReturn()
{
testEngine.Run(@"
class CoffeeMaker {
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
brew() {
print (""Enjoy your cup of "" + this.coffee);
Expand Down Expand Up @@ -331,9 +329,8 @@ public void Engine_Class_BoundMethodWithParams_InternalAndReturn()
{
testEngine.Run(@"
class CoffeeMaker {
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
brew(method) {
print (""Enjoy your cup of "" + this.coffee + "" ("" + method + "")"");
Expand All @@ -357,9 +354,8 @@ public void Engine_Class_BoundMethod_ViaReturn()
{
testEngine.Run(@"
class CoffeeMaker {
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
brew() {
print (""Enjoy your cup of "" + this.coffee);
Expand Down Expand Up @@ -387,9 +383,8 @@ public void Engine_Class_BoundMethod_ViaReturn_InOtherObject()
{
testEngine.Run(@"
class CoffeeMaker {
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
brew() {
print (""Enjoy your cup of "" + this.coffee);
Expand Down Expand Up @@ -427,9 +422,8 @@ public void FunctionInField_WhenAssignedAndCalled_ShouldReturnExpected()
testEngine.Run(@"
class CoffeeMaker {
var brew;
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
}
var maker = CoffeeMaker(""coffee and chicory"");
Expand All @@ -449,9 +443,8 @@ public void Engine_Class_Init_Simple1()
{
testEngine.Run(@"
class CoffeeMaker {
init(_coffee) {
this.coffee = _coffee;
}
var coffee;
init(coffee) {}
brew() {
print (""Enjoy your cup of "" + this.coffee);
Expand Down Expand Up @@ -718,7 +711,7 @@ class V
}

[Test]
public void Init_WhenCreatingField_ShouldSucceed()
public void Init_AttempCreateField_ShouldFail()
{
testEngine.Run(@"
class T
Expand All @@ -729,7 +722,7 @@ class T
var t = T();
print(t.a);");

Assert.AreEqual("1", testEngine.InterpreterResult);
StringAssert.StartsWith("Attempted to create a new entry", testEngine.InterpreterResult);
}

[Test]
Expand All @@ -738,7 +731,7 @@ public void Method_WhenAccessingSelfField_ShouldSucceed()
testEngine.Run(@"
class T
{
init(){this.name = ""name"";}
var name = ""name"";
Say(){print (this.name);}
}
var t = T();
Expand All @@ -753,7 +746,7 @@ public void Method_WhenAssigningExistingFieldFromArg_ShouldSucceed()
testEngine.Run(@"
class T
{
init(){this.a = 1;}
var a = 1;
Set(v)
{
this.a = v;
Expand All @@ -773,7 +766,7 @@ public void Method_WhenAssigningExistingFieldFromConst_ShouldSucceed()
testEngine.Run(@"
class T
{
init(){this.a = 1;}
var a = 1;
Set()
{
this.a = 7;
Expand All @@ -793,7 +786,7 @@ public void Method_WhenAssigningExistingFieldFromConstAndInternalPrint_ShouldSuc
testEngine.Run(@"
class T
{
init(){this.a = 1;}
var a = 1;
Set()
{
this.a = 7;
Expand Down
15 changes: 0 additions & 15 deletions ulox/ulox.core.tests/FreezeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ class Foo
StringAssert.StartsWith("Attempted to create a new ", testEngine.InterpreterResult);
}

[Test]
public void InstanceFromClass_WhenHasInitAndNoVars_ShouldSucceed()
{
testEngine.Run(@"
class CoffeeMaker
{
init(_a) { this.a = _a; }
}
var maker = CoffeeMaker(""black"");
print(maker.a);");

Assert.AreEqual("black", testEngine.InterpreterResult);
}

[Test]
public void Class_WhenFrozenAndNonExistingFieldWritten_ShouldPreventChangeAndLog()
{
Expand Down
6 changes: 3 additions & 3 deletions ulox/ulox.core.tests/NativeCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public void Run_WhenNativeFuncWithMultipleArgs_ShouldReceiveAllInOrder()
NativeCallResult Func(Vm vm)
{
var index = 0;
var a = vm.GetNextArg(ref index).val.asString;
var b = vm.GetNextArg(ref index).val.asString;
var c = vm.GetNextArg(ref index).val.asString;
var a = vm.GetArg(++index).val.asString;
var b = vm.GetArg(++index).val.asString;
var c = vm.GetArg(++index).val.asString;
vm.SetNativeReturn(0, Value.New($"Hello, {a}, {b}, and {c}, I'm native."));
return NativeCallResult.SuccessfulExpression;
}
Expand Down
5 changes: 3 additions & 2 deletions ulox/ulox.core.tests/uloxs/Samples/06-Class Features.ulox
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// init is called when an instance is created
class WithInitVal
{
var someValue;
init(val)
{
// this keyword gets access to the instance currently running the method
this.val = val;
this.someValue = val;
}
}

var foo = WithInitVal(7);
print(foo.val);
print(foo.someValue);

// class vars are initialised prior to init
class WithVars
Expand Down
2 changes: 2 additions & 0 deletions ulox/ulox.core.tests/uloxs/Tests/Classes.ulox
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Method

class WithInit
{
var a,b;

init(a,b)
{
this.a = a;
Expand Down
4 changes: 2 additions & 2 deletions ulox/ulox.core/Package/Runtime/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private void Setup()
new EnumTypeCompliette()
);

this.AddDeclarationCompilette(
(TokenType.FUNCTION, FunctionDeclaration));

AddDeclarationCompilette(new CompiletteAction(TokenType.FUNCTION, FunctionDeclaration));

this.AddStatementCompilette(
new ReturnStatementCompilette(),
Expand Down
12 changes: 1 addition & 11 deletions ulox/ulox.core/Package/Runtime/Compiler/CompilerExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
{
public static class CompilerExt
{
public static void AddDeclarationCompilette(this Compiler comp, (TokenType match, System.Action<Compiler> action) processAction)
{
comp.AddDeclarationCompilette(new CompiletteAction(processAction.match, processAction.action));
}

public static void AddDeclarationCompilette(this Compiler comp, params ICompilette[] compilettes)
{
foreach (var item in compilettes)
Expand All @@ -15,16 +10,11 @@ public static void AddDeclarationCompilette(this Compiler comp, params ICompilet
}
}

public static void AddStatementCompilette(this Compiler comp, (TokenType match, System.Action<Compiler> action) processAction)
{
comp.AddStatementCompilette(new CompiletteAction(processAction.match, processAction.action));
}

public static void AddStatementCompilette(this Compiler comp, params (TokenType match, System.Action<Compiler> action)[] processActions)
{
foreach (var item in processActions)
{
comp.AddStatementCompilette(item);
comp.AddStatementCompilette(new CompiletteAction(item.match, item.action));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace ULox
{
Expand All @@ -24,8 +25,8 @@ public ClassTypeCompilette()
_innerDeclarationCompilettes = new Dictionary<TokenType, (TypeCompiletteStage, Action<Compiler>)>()
{
{ TokenType.STATIC, (TypeCompiletteStage.Static, StaticElement) },
{ TokenType.INIT, (TypeCompiletteStage.Init, c => CompileMethod(c, FunctionType.Init)) },
{ TokenType.VAR, (TypeCompiletteStage.Var, Property) },
{ TokenType.INIT, (TypeCompiletteStage.Init, c => CompileMethod(c, FunctionType.Init)) },
{ TokenType.MIXIN, (TypeCompiletteStage.Mixin, Mixin) },
{ TokenType.SIGNS, (TypeCompiletteStage.Signs, Signs) },
};
Expand Down Expand Up @@ -87,6 +88,19 @@ private void CompileMethod(Compiler compiler, FunctionType functionType)

if (functionType == FunctionType.Init)
{
foreach (var argId in compiler.CurrentCompilerState.chunk.ArgumentConstantIds)
{
var argName = compiler.CurrentCompilerState.chunk.ReadConstant(argId).val.asString.String;
if (CurrentTypeInfoEntry.Fields.FirstOrDefault(x => x == argName) != null)
{
var (_1,_2, id) = compiler.ResolveNameLookupOpCode(argName);
compiler.EmitPacket(new ByteCodePacket(OpCode.GET_LOCAL, (byte)0));
compiler.EmitPacket(new ByteCodePacket(OpCode.GET_LOCAL, (byte)id));
compiler.EmitPacket(new ByteCodePacket(OpCode.SET_PROPERTY, (byte)argId));
compiler.EmitPop();
}
}

if (returnCount != 0)
compiler.ThrowCompilerException("Init functions cannot specify named return vars.");
}
Expand Down
2 changes: 1 addition & 1 deletion ulox/ulox.core/Package/Runtime/Engine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class Engine
public Engine(Context executionContext)
{
Context = executionContext;
Context.Vm.SetEngine(this);
Context.Vm.Engine = this;
Context.AddLibrary(new StdLibrary());
}

Expand Down
1 change: 0 additions & 1 deletion ulox/ulox.core/Package/Runtime/Engine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public sealed class Program

public List<CompiledScript> CompiledScripts { get; } = new List<CompiledScript>();


public string Disassembly
{
get
Expand Down
Loading

0 comments on commit cb62014

Please sign in to comment.