Пример #1
0
        public override Expression Call(Expression expr)
        {
            var currentProcess = Process.GetCurrentProcess();

            TimeSpan _userProcessorTimeStart;
            TimeSpan _userProcessorTimeEnd;
            TimeSpan _privilegedProcessorTimeStart;
            TimeSpan _privilegedProcessorTimeEnd;

            /* RECORD START TIME */
            Stopwatch _stopwatch = Stopwatch.StartNew();
            _userProcessorTimeStart = currentProcess.UserProcessorTime;
            _privilegedProcessorTimeStart = currentProcess.PrivilegedProcessorTime;

            /* DO THE ACTUAL OPERATION */
            var result = Evaluate(expr.Elements.Second());

            /* RECORD STOP TIME */
            _userProcessorTimeEnd = currentProcess.UserProcessorTime;
            _privilegedProcessorTimeEnd = currentProcess.PrivilegedProcessorTime;
            _stopwatch.Stop();

            /* CALCULATE ELAPSED TIME */
            var realTime = _stopwatch.ElapsedMilliseconds / 1000d;
            var userTime = GetDuration(_userProcessorTimeStart, _userProcessorTimeEnd);
            var systemTime = GetDuration(_privilegedProcessorTimeStart, _privilegedProcessorTimeEnd);

            Output(realTime, userTime, systemTime);

            return result;
        }
Пример #2
0
        public override Expression Call(Expression expr)
        {
            if (expr.Elements.Count < 4)
                throw new MistException("defmacro must have at least 3 arguments (symbol, parameters list, and a body)");

            var symbol = expr.Elements.Second();

            if (symbol.Token.Type != Tokens.SYMBOL)
                throw new MistException(string.Format("The first argument to defmacro does not evaluate to a symbol ({0})", symbol.Token));

            var parameters = expr.Elements.Third() as ListExpression;

            if (parameters == null)
                throw new MistException("The second argument to defmacro must be a list");

            var name = symbol.Token.Text;

            var macro = new Macro(
                symbol: name,
                formalParams: parameters,
                body: expr.Elements.Skip(3),
                environment: Environment);

            Environment.CurrentScope.AddBinding(symbol, macro);

            return macro;
        }
Пример #3
0
        public override Expression Call(Expression expr)
        {
            if (expr.Elements.Count != 2 || expr.Elements.Second().Token.Type != Tokens.STRING)
                throw new MistException("Load takes a single string argument.");

            var source = Environment.EvaluateString("(slurp " + expr.Elements.Second().ToString() + ")");
            return Environment.EvaluateString(source.Value.ToString());
        }
Пример #4
0
        public void AddBinding(Expression expr)
        {
            //TODO: should probably store the symbol here as well
            // will make it possible to retrieve the doc string from the symbol instead of the value
            // see AllBindings as well

            AddBinding(new SymbolExpression(expr.Token.Text), expr);
        }
Пример #5
0
        public Lambda(Expression expr, Environment environment)
            : base("anonymous", environment.CurrentScope)
        {
            _environment = environment;
            _formalParameters = new FormalParameters(expr.Elements.Second());

            Precondition = args => args.Count() == _formalParameters.Count;
            Implementation = args => expr.Elements.Skip(2).Evaluate(_environment.CurrentScope);
        }
Пример #6
0
        public FormalParameters(Expression expr)
        {
            if (!(expr is ListExpression))
                throw new MistException("Formal parameters must be a list, not " + expr);

            if (!expr.Elements.All(p => p.Token.Type == Tokens.SYMBOL))
                throw new MistException("Only symbols allowed in formal paremeters. " + expr);

            _parameters = (ListExpression)expr;
        }
Пример #7
0
        public override Expression Call(Expression expr)
        {
            var symbol = expr.Elements.Second();

            if (symbol.Token.Type != Tokens.SYMBOL)
                throw new MistException(string.Format("The first argument to set! does not evaluate to a symbol ({0})", symbol.Token));

            Expression value = Evaluate(expr.Elements.Third());

            Environment.CurrentScope.UpdateBinding(symbol, value);

            return value;
        }
Пример #8
0
        public override Expression Call(Expression expr)
        {
            var bindings = expr.Elements.Second().Elements;
            var tempScope = new Bindings() { ParentScope = Environment.CurrentScope };

            for (int i = 0; i < bindings.Count - 1; i = i + 2)
                tempScope.AddBinding(
                    bindings[i],
                    bindings[i + 1].Evaluate(tempScope));

            return Environment.WithScope(tempScope,
                () => expr.Elements.Skip(2).Evaluate(tempScope));
        }
Пример #9
0
        public void UpdateBinding(Expression symbol, Expression value)
        {
            string lookupKey = symbol.Token.Text;

            var k = _symbolBindings.FirstOrDefault(kv => kv.Key.Token.Text == lookupKey).Key;
            if (k != null)
                _symbolBindings[k] = value;

            else if (ParentScope != null)
                ParentScope.UpdateBinding(symbol, value);

            else
                throw new SymbolResolveException(lookupKey);
        }
Пример #10
0
        public override Expression Call(Expression expr)
        {
            var forms = expr.Elements.Skip(1).ToList();

            if (forms.Count() % 2 != 0)
                throw new MistException("COND requires an even number of forms");

            for (int i = 0; i < forms.Count() - 1; i = i + 2)
            {
                var condition = forms[i].Evaluate(Environment.CurrentScope);
                if (condition.IsTrue)
                    return forms[i + 1].Evaluate(Environment.CurrentScope);
            }

            return NIL.Instance;
        }
Пример #11
0
        public override Expression Call(Expression expr)
        {
            //if (expr.Elements.Count != 2 || expr.Elements.Second().Token.Type != Tokens.STRING)
            //    throw new MistException("Load takes a single string argument.");

            //var source = Environment.EvaluateString("(slurp " + expr.Elements.Second().ToString() + ")");

            // WORK IN PROGRESS !!!!

            var typeName = expr.Elements[1].Token.Text;

            Type type = Type.GetType(typeName);

            object instance = Activator.CreateInstance(type);

            return instance.ToExpression();
        }
Пример #12
0
        public override Expression Call(Expression expr)
        {
            var symbol = expr.Elements.Second();

            if (symbol.Token.Type != Tokens.SYMBOL)
                throw new MistException(string.Format("The first argument to def does not evaluate to a symbol ({0})", symbol.Token));

            Expression value = null;

            if (expr.Elements.Count == 3)
            {
                value = Evaluate(expr.Elements.Third());
            }
            else if (expr.Elements.Count == 4 && expr.Elements.Third().Token.Type == Tokens.STRING)
            {
                value = Evaluate(expr.Elements.Forth());
                value.DocString = Evaluate(expr.Elements.Third()); // as StringExpression;
            }
            else
                throw new MistException("Special form 'def' needs 2 parameters (+ an optional doc string), not " + (expr.Elements.Count - 1));

            Environment.CurrentScope.AddBinding(symbol, value);
            return symbol;
        }
Пример #13
0
 public Functionoid(Expression e)
 {
     _objectExpression = e;
 }
Пример #14
0
 public void AddBinding(Expression symbol, Expression expr)
 {
     _symbolBindings.Add(symbol, expr);
 }
Пример #15
0
 public static void PRINT(Expression exp)
 {
     Console.WriteLine(exp);
 }
Пример #16
0
 public static bool CanWrap(Expression e)
 {
     return !(e is ListExpression)
         && !(e is Function)
         && e.Value != null;
 }
Пример #17
0
 public void UpdateReplMemory(Expression expr)
 {
     _global.UpdateBinding(m3, _global.Resolve(m2.Token.Text));
     _global.UpdateBinding(m2, _global.Resolve(m1.Token.Text));
     _global.UpdateBinding(m1, expr);
 }
Пример #18
0
 public static Expression CallSpecialForm(Expression expr)
 {
     return _formsMap[expr.Elements.First().Token.Text](expr);
 }
Пример #19
0
 protected void Evaluate(string source)
 {
     interpreter = new Interpreter();
     result = interpreter.EvaluateString(source);
 }
Пример #20
0
 public override Expression Call(Expression expr)
 {
     return new Lambda(expr, Environment);
 }
Пример #21
0
 /// <summary>
 /// Delegates evaluation to expression itself, using current scope
 /// </summary>
 /// <param name="expr"></param>
 /// <returns></returns>
 public Expression Evaluate(Expression expr)
 {
     return expr.Evaluate(Environment.CurrentScope);
 }
Пример #22
0
 public override Expression Call(Expression expr)
 {
     return expr.Elements.Second();
 }
Пример #23
0
 public static Expression Call(this Function f, Expression expr)
 {
     return f.Call(expr.AsEnumerable());
 }
Пример #24
0
 private bool IsMacro(Expression expr, Bindings scope)
 {
     return expr is SymbolExpression
         && scope.Resolve(expr.Token.Text) is Macro;
 }
Пример #25
0
 public abstract Expression Call(Expression expr);