Fix read_from_token and atom
This commit is contained in:
parent
cce68e439d
commit
fff575908c
1 changed files with 16 additions and 27 deletions
|
@ -1,19 +1,19 @@
|
||||||
// Lispts: Scheme Interpreter in TypeScript
|
// Lispts: Scheme Interpreter in TypeScript
|
||||||
const program = '(begin (define r 10) (* pi (* r r)))'
|
const program = "(begin (define r 10) (* pi (* r r)))"
|
||||||
|
|
||||||
type LSymbol = Symbol // A Lisp Symbol(alias TSymbol) is implemented as TypeScript string
|
type LSymbol = string // A Lisp Symbol(alias TSymbol) is implemented as TypeScript string
|
||||||
type LNumber = number // A Lisp Symbol(alias TSymbol) is implemented as TypeScript number
|
type LNumber = number // A Lisp Symbol(alias TSymbol) is implemented as TypeScript number
|
||||||
type Atom = LSymbol | number // A Lisp Atom is a Symbol or Number impl
|
type Atom = LSymbol | number // A Lisp Atom is a Symbol or Number impl
|
||||||
type List = Array<any> // A Lisp List is implemented as a TypeScript array
|
type List = Array<any> // A Lisp List is implemented as a TypeScript array
|
||||||
type Exp = Atom | List // A Lisp expression is an Atom or List
|
type Exp = Atom | List // A Lisp expression is an Atom or List
|
||||||
type Env = Object // A Lisp environment is a mapping of {variable: value}
|
interface Env {
|
||||||
|
[key: string | number]: any
|
||||||
|
} // A Lisp environment is a mapping of {variable: value}
|
||||||
|
|
||||||
class ValueError extends Error{
|
/**
|
||||||
constructor(message:string) {
|
* Read a Scheme expression from a string.
|
||||||
super(message)
|
*/
|
||||||
this.name = 'ValueError'
|
const parse = (program: string): Exp => read_from_tokens(tokenize(program))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a string into a list of tokens.
|
* Convert a string into a list of tokens.
|
||||||
|
@ -28,40 +28,29 @@ const tokenize = (char: string): string[] =>
|
||||||
/**
|
/**
|
||||||
* Numbers become numbers; every other token is a symbol(LSymbol).
|
* Numbers become numbers; every other token is a symbol(LSymbol).
|
||||||
*/
|
*/
|
||||||
const atom = (token: string): Atom => {
|
const atom = (token: string): Atom =>
|
||||||
try {
|
Number.isNaN(token) ? Number(token) : (token as LSymbol)
|
||||||
if (Number.isNaN(token)) {
|
|
||||||
throw new ValueError('Value Error')
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
if (error === ValueError) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read an expression from a sequence of tokens
|
* Read an expression from a sequence of tokens
|
||||||
*/
|
*/
|
||||||
const read_from_tokens = (tokens: string[]): Exp => {
|
const read_from_tokens = (tokens: string[]): Exp => {
|
||||||
if (tokens.length === 0) {
|
if (tokens.length === 0 || !tokens) {
|
||||||
throw new Error('unexpected EOF while reading')
|
throw new Error('unexpected EOF while reading')
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = tokens.shift()
|
const token = tokens.shift()
|
||||||
if (token === '(') {
|
if (token === '(') {
|
||||||
const l = []
|
const l = []
|
||||||
while (token[0] !== ')') {
|
while (tokens[0] !== ')') {
|
||||||
l.push(read_from_tokens(tokens))
|
l.push(read_from_tokens(tokens))
|
||||||
}
|
}
|
||||||
tokens.shift()
|
tokens.shift()
|
||||||
return l
|
return l
|
||||||
} else if (token === ')') {
|
} else if (token === ')') {
|
||||||
throw new Error ('unexpected )')
|
throw new Error('unexpected )')
|
||||||
} else {
|
} else {
|
||||||
return atom(token)
|
return atom(token as string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(parse(program))
|
||||||
console.log(tokenize(program))
|
|
||||||
|
|
Loading…
Reference in a new issue