Skip to content

Commit

Permalink
Get prelude working
Browse files Browse the repository at this point in the history
The “externalSyms” argument, defaulting to “globals”, for ArkState.run() is
now an ArkObject.

Return an object from prelude.ursa, and, in the front-end, merge it into
‘globals’ before executing code.
  • Loading branch information
rrthomas committed Nov 29, 2023
1 parent da5083b commit 7585064
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## Version 0.2

* `range()` iterator, written in Ursa (start of stdlib/prelude).
* Sets: use `{ Exp, }` notation, with mandatory first comma.
* Map interface to file system and internet.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"scripts": {
"lint": "eslint . --ext .ts && depcheck",
"prebuild": "node --print \"'export default \\'' + require('./package.json').version + '\\';'\" > src/version.ts",
"build": "tsc --build",
"build": "tsc --build && cp src/prelude.ursa lib/",
"clean": "tsc --build --clean",
"test": "npm run build && ava",
"test-ursa": "ava src/ursa/ursa.test.ts",
Expand Down
14 changes: 12 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import path from 'path'
import fs, {PathOrFileDescriptor} from 'fs'
import {fileURLToPath} from 'url'
import * as readline from 'node:readline'
import {ArgumentParser, RawDescriptionHelpFormatter} from 'argparse'
import assert from 'assert'

import {
debug, ArkState, toJs,
ArkUndefined, ArkList, ArkValRef, ArkString, globals,
ArkUndefined, ArkNull, ArkObject, ArkList, ArkValRef, ArkString, globals,
Environment, PartialCompiledArk, compile as arkCompile, serializeVal,
} from '@ursalang/ark'

Expand Down Expand Up @@ -142,6 +143,15 @@ async function main() {
source = source.substring(source.indexOf('\n'))
}
}
const thisDir = path.dirname(fileURLToPath(import.meta.url))
const prelude = ursaCompile(
fs.readFileSync(path.join(thisDir, 'prelude.ursa'), {encoding: 'utf-8'}),
)
const ark = new ArkState()
const preludeObj = prelude.value.eval(ark) as ArkObject
for (const [sym, val] of preludeObj.val) {
globals.set(sym, new ArkValRef(val))
}
if (args.compile) {
if (source === undefined) {
throw new Error('--compile given, but nothing to compile')
Expand All @@ -158,7 +168,7 @@ async function main() {
// Run the program
let result
if (source !== undefined) {
result = runWithTraceback(new ArkState(), compile(source))
result = runWithTraceback(ark, compile(source))
}
if (source === undefined || args.interactive) {
result = await repl()
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export function compile(
const ast = semantics(matchResult)
const compiled = ast.toAST(env, false, false, false)
const freeVars = ast.freeVars(env)
env.externalSyms.forEach((_val, id) => freeVars.delete(id))
env.externalSyms.val.forEach((_val, id) => freeVars.delete(id))
return new PartialCompiledArk(compiled, freeVars, ast.boundVars)
}

Expand Down
14 changes: 7 additions & 7 deletions src/prelude.ursa
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// Released under the MIT license.

// Range iterator.
let range = fn(n) {
let i = 0
fn() {
i := i + 1
if i <= n { i - 1 } else { null }
{
range: fn(n) {
let i = 0
fn() {
i := i + 1
if i <= n { i - 1 } else { null }
}
}
}

;

0 comments on commit 7585064

Please sign in to comment.