Example #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;
        }
Example #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;
        }
Example #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());
        }
Example #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);
        }
Example #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);
        }
Example #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;
        }
Example #7
0
File: Set.cs Project: tormaroe/mist
        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;
        }
Example #8
0
File: Let.cs Project: tormaroe/mist
        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));
        }
Example #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);
        }
Example #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;
        }
Example #11
0
File: New.cs Project: tormaroe/mist
        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();
        }
Example #12
0
File: Def.cs Project: tormaroe/mist
        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;
        }
Example #13
0
 public Functionoid(Expression e)
 {
     _objectExpression = e;
 }
Example #14
0
 public void AddBinding(Expression symbol, Expression expr)
 {
     _symbolBindings.Add(symbol, expr);
 }
Example #15
0
 public static void PRINT(Expression exp)
 {
     Console.WriteLine(exp);
 }
Example #16
0
 public static bool CanWrap(Expression e)
 {
     return !(e is ListExpression)
         && !(e is Function)
         && e.Value != null;
 }
Example #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);
 }
Example #18
0
 public static Expression CallSpecialForm(Expression expr)
 {
     return _formsMap[expr.Elements.First().Token.Text](expr);
 }
Example #19
0
 protected void Evaluate(string source)
 {
     interpreter = new Interpreter();
     result = interpreter.EvaluateString(source);
 }
Example #20
0
File: Fn.cs Project: tormaroe/mist
 public override Expression Call(Expression expr)
 {
     return new Lambda(expr, Environment);
 }
Example #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);
 }
Example #22
0
 public override Expression Call(Expression expr)
 {
     return expr.Elements.Second();
 }
Example #23
0
 public static Expression Call(this Function f, Expression expr)
 {
     return f.Call(expr.AsEnumerable());
 }
Example #24
0
 private bool IsMacro(Expression expr, Bindings scope)
 {
     return expr is SymbolExpression
         && scope.Resolve(expr.Token.Text) is Macro;
 }
Example #25
0
 public abstract Expression Call(Expression expr);