Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Completely rebuilt keyword grammar. I may regret this later.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeattie committed Jul 12, 2024
1 parent e9a83c9 commit bd8b85d
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 168 deletions.
20 changes: 16 additions & 4 deletions Starship/Rockstar.Engine/Result.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
using Rockstar.Engine.Values;

namespace Rockstar.Engine;

public class Result {
public static readonly Result Ok = new();
public static readonly Result Unknown = new();
}
public enum WhatToDo {
Unknown,
Next,
Skip,
Break,
Return
}

public class Result(Value value, WhatToDo whatToDo = WhatToDo.Next) {
public Value Value => value;
public WhatToDo WhatToDo => whatToDo;
public static Result Unknown = new(new Null(), WhatToDo.Unknown);
public static Result Return(Value value) => new Result(value, WhatToDo.Return);
}
33 changes: 25 additions & 8 deletions Starship/Rockstar.Engine/RockstarEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ private void CopyVariablesFrom(RockstarEnvironment that) {
}

protected RockstarIO IO = io;

public string? ReadInput() => IO.Read();
public void WriteLine(string? output) => IO.Write((output ?? String.Empty)+ Environment.NewLine);
public void WriteLine(string? output) => IO.Write((output ?? String.Empty) + Environment.NewLine);
public void Write(string output) => IO.Write(output);
private Variable? pronounTarget;

Expand All @@ -42,7 +42,7 @@ public Result SetVariable(Variable variable, Value value) {
variables[variable.Key] = value;
}

return Result.Ok;
return new(value);
}

public Value GetVariable(Variable variable) {
Expand All @@ -52,7 +52,10 @@ public Value GetVariable(Variable variable) {

public Result Exec(Block block) {
var result = Result.Unknown;
foreach (var statement in block.Statements) result = Exec(statement);
foreach (var statement in block.Statements) {
result = Exec(statement);
if (result.WhatToDo == WhatToDo.Return) return result;
}
return result;
}

Expand All @@ -64,9 +67,23 @@ public Result Exec(Block block) {
Decrement dec => Decrement(dec),
Loop loop => Loop(loop),
FunctionCall call => Call(call),
Return r => Return(r),
Listen l => Listen(l),
_ => throw new($"I don't know how to execute {statement.GetType().Name} statements")
};

private Result Listen(Listen l) {
var input = ReadInput();
Value value = input == default ? new Null() : new Strïng(input);
if (l.Variable != default) SetVariable(l.Variable, value);
return new(value);
}

private Result Return(Return r) {
var value = Eval(r.Expression);
return Result.Return(value);
}

private Result Call(FunctionCall call) {
var value = GetVariable(call.Function);
if (value is not Function function) throw new($"'{call.Function.Name}' is not a function");
Expand All @@ -91,15 +108,15 @@ private Result Loop(Loop loop) {
private Result Increment(Increment inc) {
return Eval(inc.Variable) switch {
Number n => Assign(inc.Variable, new Number(n.Value + inc.Multiple)),
Booleän b => inc.Multiple % 2 == 0 ? Result.Ok : Assign(inc.Variable, b.Negate),
Booleän b => inc.Multiple % 2 == 0 ? new(b) : Assign(inc.Variable, b.Negate),
{ } v => throw new($"Cannot increment '{inc.Variable.Name}' because it has type {v.GetType().Name}")
};
}

private Result Decrement(Decrement dec) {
return Eval(dec.Variable) switch {
Number n => Assign(dec.Variable, new Number(n.Value - dec.Multiple)),
Booleän b => dec.Multiple % 2 == 0 ? Result.Ok : Assign(dec.Variable, b.Negate),
Booleän b => dec.Multiple % 2 == 0 ? new(b) : Assign(dec.Variable, b.Negate),
{ } v => throw new($"Cannot increment '{dec.Variable.Name}' because it has type {v.GetType().Name}")
};
}
Expand All @@ -117,13 +134,12 @@ private Result Assign(Assign assign)
private Result Output(Output output) {
var value = Eval(output.Expr);
WriteLine(value.ToStrïng().Value);
return Result.Ok;
return new(value);
}

private Value Eval(Expression expr) => expr switch {
Value value => value,
Binary binary => binary.Resolve(Eval),

// not sure what the difference is here.... ?
Looküp lookup => GetVariable(lookup.Variable),
Variable v => GetVariable(v),
Expand All @@ -132,6 +148,7 @@ private Result Output(Output output) {
{ Op: Operator.Not } => Booleän.Not(Eval(u.Expr)),
_ => throw new NotImplementedException($"Cannot apply {u.Op} to {u.Expr}")
},
FunctionCall call => Call(call).Value,
_ => throw new NotImplementedException($"Eval not implemented for {expr.GetType()}")
};
}
10 changes: 10 additions & 0 deletions Starship/Rockstar.Engine/Statements/Return.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
using System.Runtime.CompilerServices;
using Rockstar.Engine.Expressions;

namespace Rockstar.Engine.Statements;

public class Return(Expression expr, Source source)
: Statement(source) {
public Expression Expression => expr;
}

public class Listen(Source source)
: Statement(source) {
public Variable? Variable { get; init; } = default;
public Listen(Variable variable, Source source) : this(source) {
this.Variable = variable;
}
}
Loading

0 comments on commit bd8b85d

Please sign in to comment.