Skip to content

Commit

Permalink
WIP: Add parser structure
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao committed Jan 9, 2025
1 parent 6f908a3 commit 7ccc1ae
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
20 changes: 16 additions & 4 deletions Src/Parser/ParseResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,31 @@ public TRes Evaluate(TEnv env) {
}
}

public abstract class Literal<T, TRes, TEnv>: IEvaluatable<TRes, TEnv> {
protected T Value;

public abstract class Literal<TRes, TEnv>: IEvaluatable<TRes, TEnv> {
public TRes Evaluate(TEnv env) {
throw new NotImplementedException();
}
}

public class IntLiteral<TRes, TEnv> : Literal<TRes, TEnv> {
public int Value { get; set; }
}

public class NumberLiteral<TRes, TEnv> : Literal<TRes, TEnv> {
public double Value { get; set; }
}

// TODO: more literals

public abstract class Symbol<TRes, TEnv>: IEvaluatable<TRes, TEnv> {
private string name;

public TRes Evaluate(TEnv env) {
throw new NotImplementedException();
}

public Symbol(string name) {
this.name = name;
}
}
} // namespace Marionette.Parser
} // namespace Marionette.Parser
50 changes: 47 additions & 3 deletions Src/Parser/Parser.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
using System;
using System.Collections;

namespace Marionette.Parser {
public class Parser {
public class Parser<TRes, TEnv> : IEnumerator<IEvaluatable<TRes, TEnv>> {
private string code;
private Utils.Position position;
private int pointer = -1;

public delegate ResultList<TRes, TEnv> ListAllocator(IEvaluatable<TRes, TEnv>[] subterms);

public delegate Literal<TRes, TEnv> LiteralAllocator(string text);

public delegate Symbol<TRes, TEnv> SymbolAllocator(string name);

public Parser(string code) {
private ListAllocator allocateList;
private LiteralAllocator allocateLiteral;
private SymbolAllocator allocateSymbol;

private IEvaluatable<TRes, TEnv>? curResult = null;

public Parser(string code, ListAllocator allocateList, LiteralAllocator allocateLiteral, SymbolAllocator allocateSymbol) {
this.code = code;
this.position = new Utils.Position(0, 1);
this.allocateList = allocateList;
this.allocateLiteral = allocateLiteral;
this.allocateSymbol = allocateSymbol;
position = new Utils.Position(0, 1);
}

private IEvaluatable<TRes, TEnv> Parse() {
throw new NotImplementedException();
}

public IEvaluatable<TRes, TEnv> Current => curResult ?? (curResult = Parse());

object IEnumerator.Current => curResult ?? (curResult = Parse());

public void Dispose() {}

public bool MoveNext() {
if (pointer >= code.Length) {
return false;
}
else {
curResult = Parse();
return true;
}
}

public void Reset() {
position = new Utils.Position(0, 1);
pointer = -1;
}
}
} // namespace Marionette.Parser

0 comments on commit 7ccc1ae

Please sign in to comment.