示例#1
0
        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);
        }
示例#2
0
        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));
        }
示例#3
0
        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);
        }
示例#4
0
        // 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));
        }
示例#5
0
        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()));
        }
示例#6
0
        private static void postProcess(Prog prog, bool checkTypes)
        {
            //new Namer(prog).Name();
            if (checkTypes)
            {
                Specer = new Specer();
                Specer.Spec(prog);
            }

            new StructureValidator(prog).Validate();
        }
示例#7
0
        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)))));
        }
示例#8
0
        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);
        }
示例#9
0
        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));
        }
示例#10
0
        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;
              * }*/
        }
示例#11
0
 public static Fn AsFn(this Exp exp, Exp subject, Prog prog)
 {
     return(exp.As <Fn>(subject, prog));
 }
示例#12
0
 public StructureValidator(Prog program)
 {
     C.Nn(program);
     prog = program;
 }
示例#13
0
 public RemarkList(Prog prog)
 {
     this.prog = prog;
 }
示例#14
0
        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));
        }
示例#15
0
 public static Env <Value> CreateValueRoot(Prog prog)
 {
     return(CreateRoot <Value>(prog, b => b));
 }
示例#16
0
 public static Bool AsBool(this Exp exp, Exp subject, Prog prog)
 {
     return(exp.As <Bool>(subject, prog));
 }
示例#17
0
 public static Value AsValue(this Exp exp, Exp subject, Prog prog)
 {
     return(exp.As <Value>(subject, prog));
 }
示例#18
0
 public static Arr AsArr(this Exp exp, Exp subject, Prog prog)
 {
     return(exp.As <Arr>(subject, prog));
 }
示例#19
0
 public static Int AsInt(this Exp exp, Exp subject, Prog prog)
 {
     return(exp.As <Int>(subject, prog));
 }
示例#20
0
 public static Env <Spec> CreateSpecRoot(Prog prog)
 {
     return(CreateRoot(prog, b => b.FixedSpec, true));
 }
示例#21
0
 public static Obj AsObj(this Exp exp, Element subject, Prog prog)
 {
     return(exp.As <Obj>(subject, prog));
 }
示例#22
0
 public static FnApply AsFnApply(this Element exp, Exp subject, Prog prog)
 {
     return(exp.As <FnApply>(subject, prog));
 }
示例#23
0
        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));
                })
            };
        }