Example #1
0
        // Helper methods

        public object CallMethod(string name, Runtime.Params parameters)
        {
            var method = this.Lookup(name, includeLocal: true);

            if (method.IsNone())
            {
                throw new Exception($"no such method {name}");
            }
            return(((ICallable)method.Get()).Call(parameters));
        }
Example #2
0
        public IList <Ast.ModuleStmt> dataTypeMacro(string name, Runtime.Struct typeValue, Runtime.Params placeholderParams)
        {
            // TODO: generic

            var stmts = new List <Ast.ModuleStmt>();

            // Add field getters
            foreach (var field in typeValue.Fields)
            {
                stmts.Add(new Ast.ModuleFunStmt(
                              field.Name,
                              new Ast.FunDefExpr(
                                  new List <Ast.ParamDef>()
                {
                    new Ast.ParamDef("o",
                                     Ast.ParamDefKind.positional,
                                     type: Optional <Ast.Expr> .Some(new Ast.Name(name)))
                },
                                  new Ast.NativeGetField(new Ast.Name("o"), field.Name)))
                          );
            }

            // Add constructor
            string constuctorName = "make" + name;

            stmts.Add(new Ast.ModuleFunStmt(
                          constuctorName,
                          new Ast.FunDefExpr(
                              typeValue.Fields.Select(field => new Ast.ParamDef(
                                                          field.Name, Ast.ParamDefKind.named,
                                                          type: Optional.Some <Ast.Expr>(new Ast.NativeValue(field.Type)))).ToList(),
                              new Ast.NativeConstruct(new Ast.Name(name),
                                                      typeValue.Fields.Select(field => (Ast.Expr) new Ast.Name(field.Name)).ToList())
                              )
                          ));

            return(stmts);
        }