Esempio n. 1
0
        private object EX_While(LispRuntimeCommand cmd, LispList program)
        {
            object retVal = null;

            if (program.items.Count < 2)
            {
                throw new LispParseException($"'{cmd.CommandName}' command must have at least 1 parameter. Line: {program.line}:{program.position}");
            }

            LispItem conditionalStatement = program.items[1];

            // body of the while loop
start:
            object conditionalResult = Run(conditionalStatement);

            CheckParameterType(cmd, 1, program, conditionalResult, typeof(bool), false);
            if ((bool)conditionalResult)
            {
                for (int i = 2; i < program.items.Count; i++)
                {
                    retVal = Run(program.items[i]);
                }
                goto start;
            }

            return(retVal);
        }
Esempio n. 2
0
        private object EX_If(LispRuntimeCommand cmd, LispList program)
        {
            CheckParameterCount(cmd, program, 2, 3);

            LispItem conditionalStatement = program.items[1];
            LispItem trueStatement        = program.items[2];

            // run the condition
            object conditionResult = Run(conditionalStatement);

            CheckParameterType(cmd, 1, program, conditionResult, typeof(bool), false);

            if ((bool)conditionResult)
            {
                return(Run(trueStatement));
            }
            else if (program.items.Count == 4)
            {
                LispItem falseStatement = program.items[3];
                return(Run(falseStatement));
            }
            else
            {
                return(null);
            }
        }
Esempio n. 3
0
        public object RunList(LispList program)
        {
            object result = null;

            LispItem first = program.items.First();

            if (first is LispSymbol)
            {
                try
                {
                    LispSymbol         sym = first as LispSymbol;
                    LispRuntimeCommand cmd;
                    if (_commands.TryGetValue(sym.name, out cmd))
                    {
                        result = cmd.Command(cmd, program);
                        _logger.LogIf(_categories.ScriptLogging, Level.Debug, $"Run {first.line}:{first.position} Cmd:{cmd.CommandName}==>{(result ?? (object)"null")}");
                    }
                    else
                    {
                        throw new LispParseException($"Unknown command symbol '{sym.name}' Line: {sym.line}:{sym.position}");
                    }
                }
                catch (Exception ex)
                {
                    if (!(ex is LispParseException))
                    {
                        throw new LispParseException($"{ex.Message} Line {first.line}:{first.position}", ex);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else if (first is LispList)
            {
                foreach (LispItem item in program.items)
                {
                    if (item is LispList)
                    {
                        result = Run(item as LispList);
                    }
                    else
                    {
                        throw new LispParseException($"Expected a LispList but got a {item.GetType().Name} Line: {item.line}:{item.position}");
                    }
                }
            }
            else
            {
                throw new LispParseException($"Can only execute lists and symbols. Line: {first.line}:{first.position}");
            }

            return(result);
        }
Esempio n. 4
0
        public object Run(LispItem item)
        {
            object v = null;

            switch (item)
            {
            case LispList ll: v = RunList(ll); break;

            case LispString ls: v = ls.value; break;

            case LispDouble ld: v = ld.value; break;

            case LispInt li: v = li.value; break;

            case LispSymbol lsym: v = lsym.name; break;

            default: v = null; break;
            }

            return(v);
        }
Esempio n. 5
0
        public T Run <T>(LispItem item)
        {
            object v = this.Run(item);

            return((T)v);
        }