/// <summary>
        /// Compile a line of Yolol into a runnable C# function
        /// </summary>
        /// <param name="line">The line of code to convert</param>
        /// <param name="lineNumber">The number of this line</param>
        /// <param name="maxLines">The max number of lines in a valid program (20, in standard Yolol)</param>
        /// <param name="maxStringLength"></param>
        /// <param name="internalVariableMap">A dictionary used for mapping variables to integers in all lines in this script</param>
        /// <param name="externalVariableMap">A dictionary used for mapping externals to integers in all lines in this script</param>
        /// <param name="staticTypes">Statically known types for variables</param>
        /// <param name="changeDetection"></param>
        /// <returns>A function which runs this line of code. Accepts two sections of memory, internal variables and external variables. Returns the line number to go to next</returns>
        public static Func <ArraySegment <Value>, ArraySegment <Value>, LineResult> Compile(
            this Line line,
            int lineNumber,
            int maxLines,
            int maxStringLength,
            InternalsMap internalVariableMap,
            ExternalsMap externalVariableMap,
            IReadOnlyDictionary <VariableName, Type>?staticTypes = null,
            bool changeDetection = false
            )
        {
            // Locate all accessed variables and load them into the maps
            var stored = new FindAssignedVariables();

            stored.Visit(line);
            var loaded = new FindReadVariables();

            loaded.Visit(line);
            foreach (var name in stored.Names.Concat(loaded.Names).Distinct())
            {
                var dict = name.IsExternal ? (Dictionary <VariableName, int>)externalVariableMap : internalVariableMap;
                if (!dict.TryGetValue(name, out _))
                {
                    dict[name] = dict.Count;
                }
            }

            return(line.Compile(lineNumber, maxLines, maxStringLength, (IReadonlyInternalsMap)internalVariableMap, externalVariableMap, staticTypes, changeDetection));
        }
        /// <summary>
        /// Compile all the lines of a Yolol program into a `CompiledProgram` object
        /// </summary>
        /// <param name="ast"></param>
        /// <param name="externals"></param>
        /// <param name="maxLines"></param>
        /// <param name="maxStringLength"></param>
        /// <param name="staticTypes"></param>
        /// <param name="changeDetection"></param>
        /// <returns></returns>
        public static CompiledProgram Compile(
            this Program ast,
            ExternalsMap externals,
            int maxLines        = 20,
            int maxStringLength = 1024,
            IReadOnlyDictionary <VariableName, Type>?staticTypes = null,
            bool changeDetection = false
            )
        {
            if (maxLines < ast.Lines.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(maxLines), "ast has more than `maxLines` lines");
            }

            var internals = new InternalsMap();

            var compiledLines = new JitLine[maxLines];

            for (var i = 0; i < maxLines; i++)
            {
                var lineNum = i + 1;
                var line    = ast.Lines.ElementAtOrDefault(i) ?? new Line(new StatementList());
                compiledLines[i] = new JitLine(line.Compile(lineNum, maxLines, maxStringLength, internals, externals, staticTypes, changeDetection));
            }

            return(new CompiledProgram(internals, compiledLines));
        }
Beispiel #3
0
 public EasyMachineState(Value[] i, Value[] e, InternalsMap internals, ExternalsMap externals, int pc, ChangeSet set)
 {
     Internals      = i;
     Externals      = e;
     InternalMap    = internals;
     ExternalMap    = externals;
     ProgramCounter = pc;
     ChangeSet      = set;
 }
        public IExecutionState Prepare(Yolol.Grammar.AST.Program program, string done)
        {
            var internalsMap = new InternalsMap();
            var externalsMap = new ExternalsMap();
            var lines        = new List <Func <ArraySegment <Value>, ArraySegment <Value>, int> >();

            for (var i = 0; i < program.Lines.Count; i++)
            {
                lines.Add(program.Lines[i].Compile(
                              i + 1,
                              Math.Max(20, program.Lines.Count),
                              internalsMap,
                              externalsMap
                              ));
            }

            return(new ExecutionState(lines, internalsMap, externalsMap, done));
        }
Beispiel #5
0
        public static IMachineState Test(string line, int lineNumber = 1, int maxStringLength = 1024, IReadOnlyDictionary <VariableName, Type>?staticTypes = null, bool changeDetection = false)
        {
            var internals = new InternalsMap();
            var externals = new ExternalsMap();

            var ast      = Parse(line);
            var compiled = ast.Lines[0].Compile(lineNumber, 20, maxStringLength, internals, externals, staticTypes, changeDetection);

            var i = new Value[internals.Count];

            Array.Fill(i, new Value((Number)0));
            var e = new Value[externals.Count];

            Array.Fill(e, new Value((Number)0));

            var r = compiled.Invoke(i, e);

            return(new EasyMachineState(i, e, internals, externals, r.ProgramCounter, r.ChangeSet));
        }
Beispiel #6
0
        public CompareInterpreter()
        {
            _ast = Parse(
                ":done++ b=97 c=89",
                ":o++ :done++",
                ":done++",
                "i=127-1 _=(i/3%1==0)*i/3>1+(i/5%1==0)*i/5>1+(i/7%1==0)*i/7>1 a=i/11%1==0",
                "_+=a*i/11>1+(i/13%1==0)*i/13>1+(i/17%1==0)*i/17>1+(i/19%1==0)*i/19>1",
                "_+=(i/23%1==0)*i/23>1+(i/29%1==0)*i/29>1+(i/31%1==0)*i/31>1a=i/37%1==0",
                "_+=a*i/37>1+(i/41%1==0)*i/41>1+(i/43%1==0)*i/43>1+(i/47%1==0)*i/47>1",
                "_+=(i/53%1==0)*i/53>1+(i/59%1==0)*i/59>1+(i/61%1==0)*i/61>1a=i/67%1==0",
                "_+=a*i/67>1+(i/71%1==0)*i/71>1+(i/73%1==0)*i/73>1+(i/79%1==0)*i/79>1",
                "_+=(i/83%1==0)*i/83>1+(i/c%1==0)*i/c>1+(i/b%1==0)*i/b>1:o+=_<1:done++",
                "z=:o :done++goto4"
                );

            _network = new Network((Number)1, (Number)2, (Number)0);
            _state   = new MachineState(_network);

            var types = new Dictionary <VariableName, Yolol.Execution.Type> {
                { new VariableName("a"), Yolol.Execution.Type.Number },
                { new VariableName("b"), Yolol.Execution.Type.Number },
                { new VariableName("c"), Yolol.Execution.Type.Number },
                { new VariableName("d"), Yolol.Execution.Type.Number },
                { new VariableName("e"), Yolol.Execution.Type.Number },
                { new VariableName("f"), Yolol.Execution.Type.Number },
            };
            var internalsPerLine = new InternalsMap();
            var externalsPerLine = new ExternalsMap();

            _compiledLines = new Func <ArraySegment <Value>, ArraySegment <Value>, LineResult> [_ast.Lines.Count];
            for (var i = 0; i < _ast.Lines.Count; i++)
            {
                _compiledLines[i] = _ast.Lines[i].Compile(i + 1, 20, 1024, internalsPerLine, externalsPerLine, types);
            }

            _internals = new Value[internalsPerLine.Count];
            _externals = new Value[externalsPerLine.Count];

            _compiledProgramLine = _ast.Compile(new ExternalsMap(), 20, 1024, types);
        }