public static Prog Load2(TextWriter outputWriter, TextWriter errorWriter, IReadOnlyList <string> rootPaths, bool checkTypes) { rootPaths = rootPaths.Select(Path.GetFullPath).ToList(); var prog = new Prog(outputWriter, errorWriter, checkTypes); var modules = new List <ClassItem>(); foreach (var fp in rootPaths) { var codeFilesPaths = getAllCodeFiles(fp, prog.RemarkList); var shortest = codeFilesPaths.OrderBy(cfp => cfp.Length).First(); var numSeparators = shortest.Split('\\').Length - 2; foreach (var cfp in codeFilesPaths) { var startIndex = "C:\\".Length; var f = cfp.Substring(startIndex, cfp.Length - ".ef".Length - startIndex); var sections = f.Split('\\').Skip(numSeparators).ToList(); var es = parseFile(prog, cfp); var moduleBody = new ClassBody(es.AsClassItems(prog.RemarkList)); addMod(sections, moduleBody, modules, prog); } } var start = new FnApply(getStartQualifiedName(modules, prog.RemarkList), new FnArguments()); var seqItems = modules.Cast <SequenceItem>().Append(start).ToList(); prog.RootElement = new FnApply( new Fn(new FnParameters(), new Sequence(seqItems)), new FnArguments()); postProcess(prog, checkTypes); return(prog); }
private static List <Element> parseFile(Prog prog, string filePath) { C.ReturnsNn(); var codeText = File.ReadAllText(filePath); var ts = new Tokenizer().Tokenize(codeText); return(new Parser(prog.RemarkList).Parse(filePath, ts)); }
public static Prog Init(TextWriter outputWriter, TextWriter errorWriter, string asIfFilePath, string codeText, bool checkTypes) { var prog = new Prog(outputWriter, errorWriter, checkTypes); var ts = new Tokenizer().Tokenize(codeText); var e = new Parser(prog.RemarkList).Parse(asIfFilePath, ts); prog.RootElement = transformToEvaluable(e, prog); postProcess(prog, checkTypes); return(prog); }
// ReSharper disable once UnusedMethodReturnValue.Global public Spec Spec(Prog program) { C.Nn(program); C.ReturnsNn(); prog = program; isImportContext = false; returns.Clear(); Env = Efekt.Env.CreateSpecRoot(prog); return(specSequenceItem(prog.RootElement, Env, VoidSpec.Instance)); }
private static SequenceItem transformToEvaluable(List <Element> es, Prog prog) { if (es.Count == 1 && es[0] is Exp exp) { return(exp); } return(new FnApply( new Fn(new FnParameters(), new Sequence(es.AsSequenceItems(prog.RemarkList))), new FnArguments())); }
private static void postProcess(Prog prog, bool checkTypes) { //new Namer(prog).Name(); if (checkTypes) { Specer = new Specer(); Specer.Spec(prog); } new StructureValidator(prog).Validate(); }
private static Let getNewModule(string name, ClassBody moduleBody, Prog prog) { var preludeImport = new Import(new Ident("prelude", TokenType.Ident)); if (name != "prelude") { moduleBody.InsertImport(preludeImport); } return(new Let(new Ident(name, TokenType.Ident), new New(new ClassBody(moduleBody.AsClassItems(prog.RemarkList))))); }
public static B As <B>(this Element element, Element subject, Prog prog) where B : class, Element { C.Nn(element, subject, prog); C.ReturnsNn(); var e = element as B; if (e != null) { return(e); } throw prog.RemarkList.ExpectedDifferentType(subject, element, typeof(B).Name); }
public static Env <TA> CreateRoot <TA>(Prog prog, Func <Builtin, TA> selector, bool buildUsages = false) where TA : class, Element { C.Nn(prog); C.ReturnsNn(); var dict = new Dictionary <Declr, EvnItem <TA> >(); foreach (var b in new Builtins(prog).Values) { var tt = b.Name.Any(ch => ch >= 'a' && ch <= 'z') ? TokenType.Ident : TokenType.Op; dict.Add(new Let(new Ident(b.Name, tt), Void.Instance), new EvnItem <TA>(selector(b), true)); } return(new Env <TA>(prog, dict, buildUsages)); }
private static int Main(string[] args) { try { Tests.Tests.RunAllTests(); if (args.Length == 0) { Console.WriteLine("Efekt interpreter. pass file(s) and folder(s) arguments to evaluate."); Console.Read(); return(1); } var cw = new ConsoleWriter(); var prog = Prog.Load2(cw, cw, args, true); var res = prog.Run(); if (res != Void.Instance) { prog.OutputPrinter.Write(res); } return(0); } catch (EfektProgramException) { return(3); } catch (EfektException) { return(2); }/* * catch (Exception ex) * { * Console.Write(ex.Message + ex.StackTrace); * return 1; * }*/ }
public static Fn AsFn(this Exp exp, Exp subject, Prog prog) { return(exp.As <Fn>(subject, prog)); }
public StructureValidator(Prog program) { C.Nn(program); prog = program; }
public RemarkList(Prog prog) { this.prog = prog; }
private static void addMod(IReadOnlyList <string> sections, ClassBody moduleBody, List <ClassItem> modules, Prog prog) { C.Nn(sections, moduleBody, modules, prog); foreach (var s in sections.Skip(1)) { var m = findParentModule(modules, s); if (m == null) { modules.AddValue(new Let(new Ident(s, TokenType.Ident), new New(new ClassBody(new List <ClassItem>())))); modules = new List <ClassItem>(); } else { modules = m.ToList(); } } var lastSection = sections.Last(); var m2 = findParentModule(modules, lastSection); if (m2 != null) { throw new Exception(); } modules.AddValue(getNewModule(lastSection, moduleBody, prog)); }
public static Env <Value> CreateValueRoot(Prog prog) { return(CreateRoot <Value>(prog, b => b)); }
public static Bool AsBool(this Exp exp, Exp subject, Prog prog) { return(exp.As <Bool>(subject, prog)); }
public static Value AsValue(this Exp exp, Exp subject, Prog prog) { return(exp.As <Value>(subject, prog)); }
public static Arr AsArr(this Exp exp, Exp subject, Prog prog) { return(exp.As <Arr>(subject, prog)); }
public static Int AsInt(this Exp exp, Exp subject, Prog prog) { return(exp.As <Int>(subject, prog)); }
public static Env <Spec> CreateSpecRoot(Prog prog) { return(CreateRoot(prog, b => b.FixedSpec, true)); }
public static Obj AsObj(this Exp exp, Element subject, Prog prog) { return(exp.As <Obj>(subject, prog)); }
public static FnApply AsFnApply(this Element exp, Exp subject, Prog prog) { return(exp.As <FnApply>(subject, prog)); }
public Builtins(Prog prog) { Values = new List <Builtin> { new Builtin("+", new List <Spec> { IntSpec.Instance, IntSpec.Instance, IntSpec.Instance }, (@params, fna) => { C.Assume(@params.Count == 2); var a = @params[0].AsInt(fna.Arguments[0], prog); var b = @params[1].AsInt(fna.Arguments[1], prog); return(new Int(a.Value + b.Value)); }), new Builtin("-", new List <Spec> { IntSpec.Instance, IntSpec.Instance, IntSpec.Instance }, (@params, fna) => { C.Assume(@params.Count == 2); var a = @params[0].AsInt(fna.Arguments[0], prog); var b = @params[1].AsInt(fna.Arguments[1], prog); return(new Int(a.Value - b.Value)); }), new Builtin("*", new List <Spec> { IntSpec.Instance, IntSpec.Instance, IntSpec.Instance }, (@params, fna) => { C.Assume(@params.Count == 2); var a = @params[0].AsInt(fna.Arguments[0], prog); var b = @params[1].AsInt(fna.Arguments[1], prog); return(new Int(a.Value * b.Value)); }), new Builtin("==", new List <Spec> { AnySpec.Instance, AnySpec.Instance, BoolSpec.Instance }, (@params, fna) => { C.Assume(@params.Count == 2); var a = @params[0].AsValue(fna.Arguments[0], prog); var b = @params[1].AsValue(fna.Arguments[1], prog); return(new Bool(a.ToCodeString() == b.ToCodeString())); }), new Builtin("<", new List <Spec> { IntSpec.Instance, IntSpec.Instance, BoolSpec.Instance }, (@params, fna) => { C.Assume(@params.Count == 2); var a = @params[0].AsInt(fna.Arguments[0], prog); var b = @params[1].AsInt(fna.Arguments[1], prog); return(new Bool(a.Value < b.Value)); }), new Builtin("print", new List <Spec> { AnySpec.Instance, VoidSpec.Instance }, (@params, fna) => { C.Assume(@params.Count == 1); var exp = @params[0]; prog.OutputPrinter.Write(exp); return(Void.Instance); }), new Builtin("at", new List <Spec> { new ArrSpec(AnySpec.Instance), IntSpec.Instance, AnySpec.Instance }, (@params, fna) => { var items = @params[0].AsArr(fna.Arguments[0], prog).Values; var at = @params[1].AsInt(fna.Arguments[1], prog); // todo check index vs length return(items[at.Value]); }), new Builtin("setAt", new List <Spec> { new ArrSpec(AnySpec.Instance), IntSpec.Instance, AnySpec.Instance, VoidSpec.Instance }, (@params, fna) => { var items = @params[0].AsArr(fna.Arguments[0], prog).Values; var at = @params[1].AsInt(fna.Arguments[1], prog); var value = @params[2].AsValue(fna.Arguments[2], prog); // todo check index vs length return(items[at.Value] = value); }), new Builtin("count", new List <Spec> { new ArrSpec(AnySpec.Instance), IntSpec.Instance }, (@params, fna) => { var items = @params[0].AsArr(fna.Arguments[0], prog).Values; return(new Int(items.Count)); }) }; }