Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/septima-lang/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@
### PR/174

- allow a dangling comma after last formal arg of an arrow function `(a,b,) => a + b`

### PR/175

- .json files can be imported `import * as b from './b.json'; b.json`
4 changes: 4 additions & 0 deletions modules/septima-lang/src/septima.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export class Septima {
if (content === undefined) {
throw new Error(`Cannot find file '${path.join(this.sourceRoot, pathFromSourceRoot)}'`)
}

if (resolvedPath.endsWith('.json')) {
content = `export let json = ${content}`
}
const sourceCode = new SourceCode(content, pathFromSourceRoot)
const scanner = new Scanner(sourceCode)
const parser = new Parser(scanner)
Expand Down
46 changes: 44 additions & 2 deletions modules/septima-lang/tests/septima.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,50 @@ describe('septima', () => {
)
})
})
describe('import', () => {
test('makes a definition from one file to be available in another file', () => {
const septima = new Septima()
const files: Partial<Record<string, string>> = {
a: `import * as b from './b'; 'sum=' + b.sum(5, 3)`,
b: `export let sum = (x,y) => x+y`,
}
expect(septima.compileSync('a', f => files[f]).execute({})).toEqual({ tag: 'ok', value: 'sum=8' })
})
test('all exported defintions are available at the import site', () => {
const septima = new Septima()
const files: Partial<Record<string, string>> = {
a: `import * as b from './b'; b.sum(b.four, b.six)`,
b: `export let sum = (x,y) => x+y; export let four = 4; export let six = 6`,
}
expect(septima.compileSync('a', f => files[f]).execute({})).toEqual({ tag: 'ok', value: 10 })
})
test('non-exported definitions become undefined', () => {
const septima = new Septima()
const files: Partial<Record<string, string>> = {
a: `import * as b from './b';\n[b.four,\nb.six]`,
b: `export let four = 4; let six = 6`,
}
expect(septima.compileSync('a', f => files[f]).execute({})).toEqual({ tag: 'ok', value: [4, undefined] })
})
test('can import from multiple files', () => {
const septima = new Septima()
const files: Partial<Record<string, string>> = {
a: `import * as b from './b';\nimport * as c from './c'\nimport * as d from './d'; [b.val, c.val, d.val]`,
b: `export let val = 100`,
c: `export let val = 20`,
d: `export let val = 3`,
}
expect(septima.compileSync('a', f => files[f]).execute({})).toEqual({ tag: 'ok', value: [100, 20, 3] })
})
test('can import a JSON file', () => {
const septima = new Septima()
const files: Partial<Record<string, string>> = {
a: `import * as b from './b.json';b.json.map(x => x*x)`,
'b.json': JSON.stringify([4, 5, 7]),
}
expect(septima.compileSync('a', f => files[f]).execute({})).toEqual({ tag: 'ok', value: [16, 25, 49] })
})
})
describe('unit', () => {
test('evaluates to an empty string if it contains only definitions', () => {
expect(run(`export let x = 5`)).toEqual('')
Expand Down Expand Up @@ -1016,8 +1060,6 @@ describe('septima', () => {
test.todo('sort') // expect(run(`[4, 10, 9, 256, 5, 300, 2, 70].sort()`)).toEqual('--')
test.todo('support file names in locations')
test.todo('string interpolation via `foo` strings')
test.todo('imports')
test.todo('optional parameters')
test.todo('optional type annotations?')
test.todo('allow redundant commas')
test.todo('left associativity of +/-')
Expand Down