代码拉取完成,页面将自动刷新
使用go语言自制解释器 该解释器会解析源代码,构建抽象语法树(AST),然后对这棵树求值。这类解释器称为树遍历(tree-walking)解释器,因为其工作方式是在AST上遍历并解释。
let five = 5;
let ten = 10;
let add = fn(x, y) {
x + y;
};
let result = add(five, ten);
let age = 1;
let name = "Monkey";
let result = 10 * (20 / 2);
-5
!true
!false
5 + 5
5 - 5
5 / 5
5 * 5
foo == bar
foo != bar
foo < bar
foo > bar
5 * (5 + 5)
((5 + 5) * 5) * 5
add(2, 3)
add(add(2, 3), add(5, 10))
max(5, add(5, (5 * 5)))
foo * bar / foobar
add(foo, bar)
let add = fn(x, y) { return x + y };
f(x, y) {return x + y}(5, 5)
(fn(x) { return x }(5) + 10) * 10
let result = if (10 > 5) { true } else { false };
result // => true
let add = fn(a, b) { return a + b; };
let add = fn(a, b) { a + b; };
add(1, 2);
let fibonacci = fn(x) {
if (x == 0) {
0
} else {
if (x == 1) {
1
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
}
let twice = fn(f, x) {
return f(f(x));
};
let addTwo = fn(x) {
return x + 2;
}
twice(addTwo, 2); // => 6
let myArray = [1, 2, 3, 4, 5];
myArray[0] // => 1
let thorsten = {"name":"Thorsten", "age":28}
thorsten["name"] // => "Thorsten"
let x = 5 + 5;
[
LET,
IDENTIFER("x"),
EQUAL_SIGN,
INTEGER(5),
PLUS_SIGN,
INTEGER(5),
SEMICOLON
]
普特拉解析法,没有将解析函数与语法规则相关联,
而是将这些函数与单个词法单元类型相关联。这个
想法的关键是,每种词法单元类型都可以具有两个
与之相关联的解析函数,具体取决于词法单元的位
置,比如是中缀还是前缀。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。