




















把以上组件拼起来,用测试案例覆盖解释器的全部运算单元。
四则混合运算: 一个嵌套表达式覆盖加、减、乘、除四种运算,相当于 =(1+2)*3 - 4/2 = 7=。
console.log("(1+2)*3 - 4/2 =", interpreter([{ type: 'sub', args: [ { type: 'mul', args: [{ type: 'add', args: [1, 2] }, 3] }, { type: 'div', args: [4, 2] } ]}], {}));
(1+2)*3 - 4/2 = 7
比较和逻辑运算: 小于/大于按序检查,=and= 短路到 null=,=or 短路到非 null=,=not 互换真假。
console.log("1<2<3 =", interpreter([{ type: 'lt', args: [1, 2, 3] }], {})); console.log("1<3<2 =", interpreter([{ type: 'lt', args: [1, 3, 2] }], {})); console.log("3>2>1 =", interpreter([{ type: 'gt', args: [3, 2, 1] }], {})); console.log("and(1,2,3) =", interpreter([{ type: 'and', args: [1, 2, 3] }], {})); console.log("and(1,null,3) =", interpreter([{ type: 'and', args: [1, null, 3] }], {})); console.log("or(null,null,42) =", interpreter([{ type: 'or', args: [null, null, 42] }], {})); console.log("not(null) =", interpreter([{ type: 'not', args: [null] }], {})); console.log("not(1) =", interpreter([{ type: 'not', args: [1] }], {}));
1<2<3 = true 1<3<2 = null 3>2>1 = true and(1,2,3) = 3 and(1,null,3) = null or(null,null,42) = 42 not(null) = true not(1) = null
条件表达式: null 为假走 else=,非 =null 为真走 =then=。
console.log("if(null) =", interpreter([ { type: 'if', cond: null, then: [{ type: 'str', value: 'yes' }], else: [{ type: 'str', value: 'no' }] } ], {})); console.log("if(gt(42,0)) =", interpreter([ { type: 'if', cond: { type: 'gt', args: [42, 0] }, then: [{ type: 'str', value: 'positive' }], else: [{ type: 'str', value: 'non-positive' }] } ], {}));
if(null) = no if(gt(42,0)) = positive
变量 + 循环: 用 while 累加求和 =1+2+...+5=。
let sumExample = [ { type: 'num', name: 'i', value: 0 }, { type: 'num', name: 'n', value: 5 }, { type: 'num', name: 'sum', value: 0 }, { type: 'while', cond: { type: 'lt', args: [{ type: 'var', name: 'i' }, { type: 'var', name: 'n' }] }, body: [ { type: 'num', name: 'i', value: { type: 'add', args: [{ type: 'var', name: 'i' }, 1] } }, { type: 'num', name: 'sum', value: { type: 'add', args: [{ type: 'var', name: 'sum' }, { type: 'var', name: 'i' }] } }, ] }, { type: 'var', name: 'sum' } ]; console.log("1+2+...+5 =", interpreter(sumExample, {}));
1+2+...+5 = 15
函数 + 闭包: 定义一个阶乘函数 fact(5)=,内部用 =while 循环。
let factExample = [ { type: 'fn', name: 'fact', value: { args: ['n'], body: [ { type: 'num', name: 'result', value: 1 }, { type: 'while', cond: { type: 'gt', args: [{ type: 'var', name: 'n' }, 0] }, body: [ { type: 'num', name: 'result', value: { type: 'mul', args: [{ type: 'var', name: 'result' }, { type: 'var', name: 'n' }] } }, { type: 'num', name: 'n', value: { type: 'sub', args: [{ type: 'var', name: 'n' }, 1] } }, ] }, { type: 'var', name: 'result' } ] } }, { type: 'call', name: 'fact', args: [5] }, ]; console.log("fact(5) =", interpreter(factExample, {}));
fact(5) = 120
字符串 + 数组: 字符串变量和数组变量都能正确定义和引用。
console.log(interpreter([
{ type: 'str', name: 'msg', value: 'Hello World!' },
{ type: 'fn', name: 'greet', value: { args: [], body: [{ type: 'var', name: 'msg' }] } },
{ type: 'call', name: 'greet', args: [] },
], {}));
console.log(interpreter([
{ type: 'vec', name: 'xs', value: [1, 2, 3] },
{ type: 'var', name: 'xs' }
], {}));
Hello World! [ 1, 2, 3 ]
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。