Skip to content

Commit

Permalink
Add some high level program states (mostly stubbed)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Jan 23, 2022
1 parent 2075a36 commit 9f25a89
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 6 deletions.
9 changes: 9 additions & 0 deletions EOBot/EOBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@
<Compile Include="Interpreter\BotToken.cs" />
<Compile Include="Interpreter\BotTokenParser.cs" />
<Compile Include="Interpreter\BotTokenType.cs" />
<Compile Include="Interpreter\States\AssignmentEvaluator.cs" />
<Compile Include="Interpreter\States\FunctionEvaluator.cs" />
<Compile Include="Interpreter\States\IScriptEvaluator.cs" />
<Compile Include="Interpreter\States\KeywordEvaluator.cs" />
<Compile Include="Interpreter\States\LabelEvaluator.cs" />
<Compile Include="Interpreter\States\ProgramState.cs" />
<Compile Include="Interpreter\States\ScriptEvaluator.cs" />
<Compile Include="Interpreter\States\StatementEvaluator.cs" />
<Compile Include="Interpreter\States\StatementListEvaluator.cs" />
<Compile Include="Interpreter\Variables\VoidFunction.cs" />
<Compile Include="Interpreter\Variables\Function.cs" />
<Compile Include="Interpreter\Variables\ICallable.cs" />
Expand Down
9 changes: 3 additions & 6 deletions EOBot/Interpreter/BotInterpreter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EOBot.Interpreter.Variables;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -8,7 +7,6 @@ namespace EOBot.Interpreter
{
public class BotInterpreter
{
private readonly Dictionary<string, IIdentifiable> _symbolTable;
private readonly BotTokenParser _parser;

public BotInterpreter(string filePath)
Expand All @@ -19,10 +17,9 @@ public BotInterpreter(string filePath)
public BotInterpreter(StreamReader inputStream)
{
_parser = new BotTokenParser(inputStream);
_symbolTable = new Dictionary<string, IIdentifiable>();
}

public List<BotToken> Parse()
public IReadOnlyList<BotToken> Parse()
{
_parser.Reset();

Expand All @@ -44,7 +41,7 @@ public List<BotToken> Parse()
return retList;
}

public async Task Run(List<BotToken> tokens)
public async Task Run(IReadOnlyList<BotToken> tokens)
{
}
}
Expand Down
22 changes: 22 additions & 0 deletions EOBot/Interpreter/States/AssignmentEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

namespace EOBot.Interpreter.States
{
public class AssignmentEvaluator : IScriptEvaluator
{
private readonly IEnumerable<IScriptEvaluator> _evaluators;

public AssignmentEvaluator(IEnumerable<IScriptEvaluator> evaluators)
{
_evaluators = evaluators;
}

public bool Evaluate(ProgramState input)
{
// match variable
// match =
// match expr
throw new System.NotImplementedException();
}
}
}
19 changes: 19 additions & 0 deletions EOBot/Interpreter/States/FunctionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;

namespace EOBot.Interpreter.States
{
public class FunctionEvaluator : IScriptEvaluator
{
private readonly IEnumerable<IScriptEvaluator> _evaluators;

public FunctionEvaluator(IEnumerable<IScriptEvaluator> evaluators)
{
_evaluators = evaluators;
}

public bool Evaluate(ProgramState input)
{
throw new System.NotImplementedException();
}
}
}
7 changes: 7 additions & 0 deletions EOBot/Interpreter/States/IScriptEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EOBot.Interpreter.States
{
public interface IScriptEvaluator
{
bool Evaluate(ProgramState input);
}
}
22 changes: 22 additions & 0 deletions EOBot/Interpreter/States/KeywordEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

namespace EOBot.Interpreter.States
{
public class KeywordEvaluator : IScriptEvaluator
{
private readonly IEnumerable<IScriptEvaluator> _evaluators;

public KeywordEvaluator(IEnumerable<IScriptEvaluator> evaluators)
{
_evaluators = evaluators;
}

public bool Evaluate(ProgramState input)
{
// evaluate if
// or evaluate while
// or evaluate goto
throw new System.NotImplementedException();
}
}
}
11 changes: 11 additions & 0 deletions EOBot/Interpreter/States/LabelEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace EOBot.Interpreter.States
{
public class LabelEvaluator : IScriptEvaluator
{
public bool Evaluate(ProgramState input)
{
return input.Expect(BotTokenType.Label)
&& input.Expect(BotTokenType.Colon);
}
}
}
50 changes: 50 additions & 0 deletions EOBot/Interpreter/States/ProgramState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using EOBot.Interpreter.Variables;
using System.Collections.Generic;

namespace EOBot.Interpreter.States
{
public class ProgramState
{
public Stack<BotToken> OperationStack { get; }

public IReadOnlyList<BotToken> Program { get; }

public Dictionary<string, IIdentifiable> SymbolTable { get; }

public Dictionary<LabelIdentifier, int> Labels { get; }

public int ExecutionIndex { get; private set; }

public ProgramState(IReadOnlyList<BotToken> program)
{
OperationStack = new Stack<BotToken>();
Program = program;
SymbolTable = new Dictionary<string, IIdentifiable>();
Labels = new Dictionary<LabelIdentifier, int>();
ExecutionIndex = 0;
}

public bool Expect(BotTokenType tokenType)
{
if (ExecutionIndex >= Program.Count)
return false;

return Program[ExecutionIndex].TokenType == tokenType;
}

public bool Match(BotTokenType tokenType)
{
if (ExecutionIndex >= Program.Count)
return false;

if (Program[ExecutionIndex].TokenType == tokenType)
{
OperationStack.Push(Program[ExecutionIndex]);
ExecutionIndex++;
return true;
}

return false;
}
}
}
24 changes: 24 additions & 0 deletions EOBot/Interpreter/States/ScriptEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Linq;

namespace EOBot.Interpreter.States
{
public class ScriptEvaluator : IScriptEvaluator
{
private readonly IEnumerable<IScriptEvaluator> _evaluators;

public ScriptEvaluator(IEnumerable<IScriptEvaluator> evaluators)
{
_evaluators = evaluators;
}

public bool Evaluate(ProgramState input)
{
return _evaluators
.OfType<StatementListEvaluator>()
.Single()
.Evaluate(input)
&& input.Expect(BotTokenType.EOF);
}
}
}
32 changes: 32 additions & 0 deletions EOBot/Interpreter/States/StatementEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;

namespace EOBot.Interpreter.States
{
public class StatementEvaluator : IScriptEvaluator
{
private readonly IEnumerable<IScriptEvaluator> _evaluators;

public StatementEvaluator(IEnumerable<IScriptEvaluator> evaluators)
{
_evaluators = evaluators;
}

public bool Evaluate(ProgramState input)
{
return Evaluate<AssignmentEvaluator>(input)
|| Evaluate<KeywordEvaluator>(input)
|| Evaluate<LabelEvaluator>(input)
|| Evaluate<FunctionEvaluator>(input);
}

private bool Evaluate<T>(ProgramState input)
where T : IScriptEvaluator
{
return _evaluators
.OfType<T>()
.Single()
.Evaluate(input);
}
}
}
24 changes: 24 additions & 0 deletions EOBot/Interpreter/States/StatementListEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Linq;

namespace EOBot.Interpreter.States
{
public class StatementListEvaluator : IScriptEvaluator
{
private readonly IEnumerable<IScriptEvaluator> _evaluators;

public StatementListEvaluator(IEnumerable<IScriptEvaluator> evaluators)
{
_evaluators = evaluators;
}

public bool Evaluate(ProgramState input)
{
return _evaluators
.OfType<StatementEvaluator>()
.Single()
.Evaluate(input)
&& this.Evaluate(input);
}
}
}

0 comments on commit 9f25a89

Please sign in to comment.