Crafting Interpreters 笔记2

The Lox Language

Automatic memory management

管理内存主要有两项技术:reference countingtracing garbage collection(又称GC),前者更易实现(PerlPHPPython),但会导致一些问题,最终还是要添加tracing GC(?)。

Logical operators

orand都是control flow constructs,因为“短路”:and左边为false就直接返回

Statements&Expressions

Where an expression’s main job is to produce a value, a statement’s job is to produce an effect

"some expression";//this is an expression statement.

Functions

argument:实参

parameter:形参

Closures

first class functions:函数可以被当作参数传递给其他函数,可以作为另一个函数的返回值,还可以被赋值给一个变量。

文中这个例子感觉不太理解(可能是它的定义的语言支持这样,whatever,我们本也想尝试写Java的解释器):

fun addPair(a, b) {
  return a + b;
}

fun identity(a) {
  return a;
}

print identity(addPair)(1, 2); // Prints "3".

当有了闭包后,变量的作用域就不仅仅是一个栈式的了。

Classes or prototypes

有两种方式实现objects

  • class-based

    • instance:store the state for each object and have a reference to the instance’s class
    • classes:contain the methods and inheritance chain

在这里插入图片描述

  • prototype-based

    • object:each individual object may contain state and methods

在这里插入图片描述

注意在lox中并不将基本类的对象当作类实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值