public static JymlType[] ParametersToArguments(this Cons cons, Jyml.Environment.JymlEnvironment env) { JymlType[] arr = new JymlType[cons.Length]; int i = 0; foreach (var item in cons) { try { if (item is string str) { arr[i] = JymlType.CreateType(str); } else if (item is JymlType jymlType) { arr[i] = jymlType; } else { arr[i] = JymlType.CreateType(item); } } catch (InvalidCastException) { arr[i] = env.GetVariableValue(item as string); } i++; } return(arr); }
/* * ;; eval-assignment 调用 eval 找出需要赋的值,将变量和得到的值传给过程 set-variable-value!, * ;; 将有关的值安置到指定环境里 * (define (eval-assignment exp env) * (set-variable-value! (assignment-variable exp) * (eval (assignment-value exp) env) * env) * 'ok) */ private static JymlAST.Cons EvalAssignment(JymlAST.Cons exp, JymlEnvironment env) { env.SetVariableValue( var: (exp.cdr as JymlAST.Cons).car as string, val: JymlType.CreateType(((exp.cdr as JymlAST.Cons).cdr as JymlAST.Cons).car as string) ); return(null); }
/* * (define (eval-definition exp env) * (define-variable! (definition-variable exp) * (eval (definition-value exp) env) * env)) */ private static JymlAST.Cons EvalDefinition(JymlAST.Cons exp, JymlEnvironment env) { /* * (define (definition-variable exp) * (if [symbol? [mcar [mcdr exp]]] * [mcar [mcdr exp]] * [mcar [mcar [mcdr exp]]])) */ string defineVariable(JymlAST.Cons c) => Parser.IsVariable(c.cdr as JymlAST.Cons) ? (c.cdr as JymlAST.Cons).car as string // 普通变量或 lambda 变量 : ((c.cdr as JymlAST.Cons).car as JymlAST.Cons).car as string; // 过程定义 /* * (define (definition-value exp) * (if [symbol? [mcar [mcdr exp]]] * [mcar [mcdr [mcdr exp]]] * (make-lambda [mcdr [mcar [mcdr exp]]] ; formal parameters * [mcdr [mcdr exp]]))) ; body */ JymlType defineValue(JymlAST.Cons c) { if (Parser.IsVariable(c.cdr as JymlAST.Cons)) { if (((c.cdr as JymlAST.Cons).cdr as JymlAST.Cons).car is string atom) { return(JymlType.CreateType(atom)); // 普通变量或 lambda 变量 } else if (((c.cdr as JymlAST.Cons).cdr as JymlAST.Cons).car is JymlAST.Cons cons) { return(JymlType.CreateType(Eval(cons, env).car)); // 返回一个 value 的子表达式(过程调用) } else { throw new Exception($"define 表达式 {exp} 的 value 部分只能是一个原子值或一个返回原子值的过程调用。"); } } else { return(new Procedures(Parser.GetProcedureName(exp), (JymlAST.Cons)Parser.GetLambdaParameters(exp), Parser.GetLambdaBody(exp), env)); // 过程定义 } } env.DefineVariable(defineVariable(exp), defineValue(exp)); return(null); }