Example #1
0
        private static async Task <object> InvokeMacro(PlasticContext context, PlasticMacro macro, IExpression[] args)
        {
            var res = await macro(context, args);

            context.Declare("last", res);
            return(res);
        }
Example #2
0
        public static void BootstrapLib(PlasticContext context)
        {
            var lib = @"
for := func (@init , @guard, @step, @body) {
    init()
    while(guard()) {
        body()
        step()
    }
}

repeat := func (times, @body) {
    while(times >= 0) {
        body()
        times--
    }
}

LinkedList := class {
    Node := class (value) { next = null; }

    head := null;
    tail := null;
    add := func (value) {
        node := Node(value);
        if (head == null) {         
            head = node;
            tail = node;
        }
        else {
            tail.next =  node;
            tail = node;  
        }        
    }

    each := func (lambda) {
        current := head;
        while(current != null) {
            lambda(current.value);
            current = current.next;
        }
    }
}

Stack := class {
    Node := class (value,prev) { next = null; }

    head := null;
    tail := null;
    push := func (value) {
        node = Node(value,tail);
        if (head == null) {         
            head = node;
            tail = node;
        }
        else {
            tail.next =  node;
            tail = node;  
        }        
    }

    each := func (lambda) {
        current = tail;
        while(current != null) {
            lambda(current.value);
            current = current.prev;
        }
    }

    peek := func() {
        tail.value;
    }

    pop := func() {
        res = tail.value;
        tail = tail.prev;
        if (tail != null) {
            tail.next = null;
        }
        else {
            head = null;
        }
        res
    }
}


switch :=  func(exp, @body) {
    matched := false;
    case := func (value, @caseBody) {   
        if (exp == value) {
            caseBody();
            matched = true;
        }
    }
    default := func (@defaultBody) {
        if (matched == false) {
            defaultBody();
        }
    }
    body();
}

quote := func(@q) {
    q
}


";


            var libCode = PlasticParser.Statements.Parse(lib);

            libCode.Eval(context);
        }
Example #3
0
        public static Task <object> Run(string code, PlasticContext context)
        {
            var res = PlasticParser.Statements.Parse(code);

            return(res.Eval(context));
        }
 private static async Task<object> InvokeMacro(PlasticContext context, PlasticMacro macro, IExpression[] args)
 {
     var res = await macro(context, args);
     context.Declare("last", res);
     return res;
 }
 public PlasticContextImpl(PlasticContext parentContext) : base(parentContext)
 {
 }
 public InstanceContext(object obj, PlasticContext owner):base(owner)
 {
     _obj = obj;
 }
 public ArrayContext(object[] array, PlasticContext owner) : base(owner)
 {
     _array = array;
 }
 public TypeContext(Type type,PlasticContext owner) : base(owner)
 {
     _type = type;
 }
 protected PlasticContext(PlasticContext parent)
 {
     Parent = parent;
 }
Example #10
0
 public PlasticContextImpl(PlasticContext parentContext) : base(parentContext)
 {
 }
Example #11
0
 public InstanceContext(object obj, PlasticContext owner) : base(owner)
 {
     _obj = obj;
 }
Example #12
0
 public ArrayContext(object[] array, PlasticContext owner) : base(owner)
 {
     _array = array;
 }
Example #13
0
 public TypeContext(Type type, PlasticContext owner) : base(owner)
 {
     _type = type;
 }
Example #14
0
 protected PlasticContext(PlasticContext parent)
 {
     Parent = parent;
 }
Example #15
0
        public static void BootstrapLib(PlasticContext context)
        {
            var lib = @"
for := func (@init , @guard, @step, @body) {
    init()
    while(guard()) {
        body()
        step()
    }
}

repeat := func (times, @body) {
    while(times >= 0) {
        body()
        times--
    }
}

LinkedList := class {
    Node := class (value) { next = null; }

    head := null;
    tail := null;
    add := func (value) {
        node := Node(value);
        if (head == null) {         
            head = node;
            tail = node;
        }
        else {
            tail.next =  node;
            tail = node;  
        }        
    }

    each := func (lambda) {
        current := head;
        while(current != null) {
            lambda(current.value);
            current = current.next;
        }
    }
}

Stack := class {
    Node := class (value,prev) { next = null; }

    head := null;
    tail := null;
    push := func (value) {
        node = Node(value,tail);
        if (head == null) {         
            head = node;
            tail = node;
        }
        else {
            tail.next =  node;
            tail = node;  
        }        
    }

    each := func (lambda) {
        current = tail;
        while(current != null) {
            lambda(current.value);
            current = current.prev;
        }
    }

    peek := func() {
        tail.value;
    }

    pop := func() {
        res = tail.value;
        tail = tail.prev;
        if (tail != null) {
            tail.next = null;
        }
        else {
            head = null;
        }
        res
    }
}


switch :=  func(exp, @body) {
    matched := false;
    case := func (value, @caseBody) {   
        if (exp == value) {
            caseBody();
            matched = true;
        }
    }
    default := func (@defaultBody) {
        if (matched == false) {
            defaultBody();
        }
    }
    body();
}

quote := func(@q) {
    q
}


";


            var libCode = PlasticParser.Statements.Parse(lib);
            libCode.Eval(context);
        }
Example #16
0
 public static Task<object> Run(string code, PlasticContext context)
 {
     var res = PlasticParser.Statements.Parse(code);
     return res.Eval(context);
 }