From 5f11930009cfd89e43a4bc8f3ed30c5363857a49 Mon Sep 17 00:00:00 2001 From: Suzanne Soy Date: Tue, 1 Sep 2020 06:55:29 +0000 Subject: [PATCH] WIP --- Compilers/JS.cs | 2 +- DefaultGrammar.cs | 2 +- Evaluator.cs | 28 +++++++++++++++++++++++----- MixFixGenerator.cs | 1 + main.cs | 8 ++++---- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Compilers/JS.cs b/Compilers/JS.cs index 995fc7d..7ac59c2 100644 --- a/Compilers/JS.cs +++ b/Compilers/JS.cs @@ -2,7 +2,7 @@ namespace Compilers { public class JS { public static string Compile(Ast.AstNode source) { return "process.stdout.write(String(" - + "\"no JS for now\"" + + "\"NO JavaScript COMPILATION FOR NOW\"" /* + source.Match( Int: i => i.ToString(), String: s => $"'{s.ToString()}'" diff --git a/DefaultGrammar.cs b/DefaultGrammar.cs index 6835e5e..7edd791 100644 --- a/DefaultGrammar.cs +++ b/DefaultGrammar.cs @@ -16,7 +16,7 @@ public static class DefaultGrammar { .WithOperator("terminal", Unsupported, NonAssociative, S.Ident) // This is the root set of operators // TODO: this needs aliases - .WithOperator("prog", Unsupported, LeftAssociative, "equality|terminal", S.And, "equality|terminal") + .WithOperator("prog", And, LeftAssociative, "equality|terminal", S.And, "equality|terminal") .WithOperator("prog", LiteralInt, NonAssociative, S.Int) .WithOperator("prog", LiteralString, NonAssociative, S.StringOpen, S.String, S.StringClose) .WithOperator("program", Program, NonAssociative, S.StartOfInput, "prog", S.EndOfInput) diff --git a/Evaluator.cs b/Evaluator.cs index 6da61bf..8e5647c 100644 --- a/Evaluator.cs +++ b/Evaluator.cs @@ -1,10 +1,17 @@ +using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using Immutable; using static Global; public class Evaluator { - public static string Evaluate(Ast.AstNode source) + public static string EvaluateWrap(Ast.AstNode source) + => Evaluate(source, ImmutableDictionary.Empty).Match( + Int: i => i.ToString(), + String: s => s); + + public static Ast.Val Evaluate(Ast.AstNode source, ImmutableDictionary env) // => Log(source.Str(), () => source.Match( Operator: o => o.Item1.semantics.Match( @@ -14,7 +21,16 @@ public class Evaluator { throw new RuntimeErrorException("The Program wrapper should contain two parts: StartOfInput, prog and EndOfInput"); } // TODO: check that the last token is indeed Program - return Evaluate(o.Item2.ElementAt(1)); + return Evaluate(o.Item2.ElementAt(1), env); + }, + And: () => { + if (o.Item2.Count() != 3) { + throw new RuntimeErrorException("The And operator should contain three parts"); + } + // TODO: check that the last token is indeed Program + var a = Evaluate(o.Item2.ElementAt(0), env); + var b = Evaluate(o.Item2.ElementAt(2), env); + return Ast.Val.Int(999); }, LiteralInt: () => o.Item2 @@ -24,7 +40,8 @@ public class Evaluator { .AsTerminal .ElseThrow( new RuntimeErrorException("LiteralInt's contents should be a lexeme")) - .lexeme, + .lexeme + .Pipe(x => Ast.Val.Int(Int32.Parse(x))), LiteralString: () => { if (o.Item2.Count() != 3) { throw new RuntimeErrorException("LiteralString should contain three lexemes: OpenString, String and CloseString"); @@ -33,10 +50,11 @@ public class Evaluator { return o.Item2.ElementAt(1) .AsTerminal.ElseThrow( new RuntimeErrorException("LiteralInt's contents should be a lexeme")) - .lexeme; + .lexeme + .Pipe(x => Ast.Val.String(x)); }, Unsupported: () => throw new RuntimeErrorException($"Unsupported opeartor {o}, sorry.")), - Terminal: t => t.lexeme/*TODO*/); + Terminal: t => Ast.Val.String(t.lexeme)/*TODO*/); } // Note: for typeclass resolution, ask that functions have their parameters and return types annotated. This annotation is added to the values at run-time, which allows to dispatch based on the annotation rather than on the actual value. \ No newline at end of file diff --git a/MixFixGenerator.cs b/MixFixGenerator.cs index 355c220..b4243eb 100644 --- a/MixFixGenerator.cs +++ b/MixFixGenerator.cs @@ -58,6 +58,7 @@ public static class ParserGenerator { Case("Program"), Case("LiteralInt"), Case("LiteralString"), + Case("And"), Case("Unsupported")), Variant("Part", diff --git a/main.cs b/main.cs index d6fd44e..1c3e182 100644 --- a/main.cs +++ b/main.cs @@ -58,8 +58,8 @@ public static class MainClass { // first-class functions by using repeated .Add() // See https://repl.it/@suzannesoy/WarlikeWorstTraining#main.cs var compilers = ImmutableList>.Empty - .Add(" js ", Compilers.JS.Compile, Exe("node")) - .Add("eval", Evaluator.Evaluate, Exe("cat")); + .Add(" js ", Compilers.JS.Compile, Exe("node")) + .Add("eval", Evaluator.EvaluateWrap, Exe("cat")); var total = 0; var passed = 0; @@ -99,8 +99,8 @@ public static class MainClass { } else { var source = args[0].File(); var destPrefix = source.DropExtension(); - CompileToFile(Compilers.JS.Compile, source, destPrefix.Combine(Ext(".js"))); - CompileToFile(Evaluator.Evaluate, source, destPrefix.Combine(Ext(".txt"))); + CompileToFile(Compilers.JS.Compile, source, destPrefix.Combine(Ext(".js"))); + CompileToFile(Evaluator.EvaluateWrap, source, destPrefix.Combine(Ext(".txt"))); Console.Write(destPrefix.Combine(Ext(".txt")).Read()); } } catch (UserErrorException e) {