Skip to content

Commit 2542306

Browse files
committed
Changes:
- Change lambda notation - Support one parameter notation for lambda
1 parent 5becf29 commit 2542306

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

LANGUAGE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ Or defining functions:
5555
returnExpression3
5656
)
5757
```
58-
`lambda` define an anonimous function:
58+
`->` a lambda define an anonimous function:
5959
```lisp
6060
(-> [param1 param2 ... paramN] expression)
61+
(-> param expression)
6162
```
6263
Function composition:
6364
```lisp

interpreter.spec.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import test = require('tape')
22
import { evalAst, makeAPI } from './interpreter'
33

44
test('interpreter', t => {
5-
t.plan(26)
5+
t.plan(27)
66

77
t.deepEqual(
88
evalAst(makeAPI({}))(['+', '1', '2', '3']),
@@ -210,18 +210,27 @@ test('interpreter', t => {
210210

211211
t.deepEqual(
212212
evalAst(makeAPI({}))(
213-
[['lambda', ['x', 'y'], ['+', 'x', 'y']], 23, 34],
213+
[['->', ['x', 'y'], ['+', 'x', 'y']], 23, 34],
214214
),
215215
['atom', 57],
216216
'Lambda definition and application'
217217
)
218218

219+
debugger
220+
t.deepEqual(
221+
evalAst(makeAPI({}))(
222+
[['->', 'param', ['+', 'param', '10']], 23],
223+
),
224+
['atom', 33],
225+
'Lambda definition and application (one parameter)'
226+
)
227+
219228
t.deepEqual(
220229
evalAst(makeAPI({}))(
221230
[
222231
['.',
223-
['lambda', ['x'], ['+', 'x', '1']],
224-
['lambda', ['x'], ['*', 'x', '3']],
232+
['->', ['x'], ['+', 'x', '1']],
233+
['->', ['x'], ['*', 'x', '3']],
225234
],
226235
3
227236
],
@@ -234,8 +243,8 @@ test('interpreter', t => {
234243
evalAst(makeAPI({}))(
235244
[
236245
['pipe',
237-
['lambda', ['x'], ['+', 'x', '1']],
238-
['lambda', ['x'], ['*', 'x', '3']],
246+
['->', ['x'], ['+', 'x', '1']],
247+
['->', ['x'], ['*', 'x', '3']],
239248
],
240249
3
241250
],

interpreter.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,19 @@ export const evalAst = api => ast => {
5656
export function evaluateFn (api, name, [params, body], args) {
5757
api._pushScope()
5858
let argsVal = args.map(api.exp)
59-
for (let i = 0, len = params.length; i < len; i++) {
60-
if (argsVal[i] !== undefined) {
61-
api.setValue(params[i], argsVal[i])
59+
if (params instanceof Array) {
60+
for (let i = 0, len = params.length; i < len; i++) {
61+
if (argsVal[i] !== undefined) {
62+
api.setValue(params[i], argsVal[i])
63+
} else {
64+
return api.evalAst(['throw', `"Missing argument '${params[i]}' in function '${name}'"`])
65+
}
66+
}
67+
} else {
68+
if (argsVal[0] !== undefined) {
69+
api.setValue(params, argsVal[0])
6270
} else {
63-
return api.evalAst(['throw', `"Missing argument '${params[i]}' in function '${name}'"`])
71+
return api.evalAst(['throw', `"Missing argument '${params[0]}' in function '${name}'"`])
6472
}
6573
}
6674
let result = api.exp(body)
@@ -136,7 +144,7 @@ export const atoms = {
136144
}
137145
},
138146
// Lambda definition
139-
lambda: (api, [params, body]) => {
147+
'->': (api, [params, body]) => {
140148
return ['fn', [params, body]]
141149
},
142150
// Function composition operator
@@ -204,8 +212,6 @@ export const atoms = {
204212
get: (api, args) => {
205213
let a = args.map(a => api.exp(a)[1])
206214
let res = a[0]
207-
console.log(a)
208-
console.log(a[0])
209215
for (let i = 1, len = a.length; i < len; i++) {
210216
res = res[a[i]]
211217
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lisp-js-compiler",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "JavaScript compiler / interpreter for Lisp language",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)