Skip to content

Commit

Permalink
Merge pull request #12 from fink-lang/latest-larix
Browse files Browse the repository at this point in the history
feat(lang): support number and identifier token
  • Loading branch information
kollhof authored Feb 28, 2020
2 parents 5684e22 + 5d104f8 commit 1e73cd9
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 69 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@babel/cli": "^7.2.3",
"@babel/plugin-proposal-pipeline-operator": "^7.3.2",
"@babel/preset-env": "^7.3.4",
"@fink/larix": "^3.0.0",
"@fink/larix": "^4.0.0",
"@nearmap/eslint-config-base": "^1.1.0",
"babel-eslint": "^10.1.0",
"commitizen": "^4.0.3",
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import babel_gen from '@babel/generator';
import traverse from '@babel/traverse';

import {other_token} from '@fink/prattler/symbols';

import {ident, expr_block, wrap} from './types';
import {code_frame_err} from './errors';

Expand Down Expand Up @@ -33,7 +31,8 @@ import {transform_unary} from './transform/unary';
import {transform_member} from './transform/member';
import {transform_inifx} from './transform/infix';
import {transform_import} from './transform/import';
import {transform_other, escape_ident, var_prefix} from './transform/other';
import {transform_ident, escape_ident, var_prefix} from './transform/other';
import {transform_number} from './transform/number';

import {
transform_jsx_elem, transform_jsx_attr, transform_jsx_str,
Expand All @@ -53,6 +52,9 @@ const jsx = {
};

const literals = {
ident: transform_ident,
number: transform_number,

string: transform_string,
regex: transform_regex,

Expand All @@ -61,9 +63,7 @@ const literals = {
array: transform_array,

object: transform_object,
prop: transform_prop,

[other_token]: transform_other
prop: transform_prop
};

const unary_ops = {
Expand Down
34 changes: 34 additions & 0 deletions src/transform/__snapshots__/number.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`numbers transforms floats 1`] = `
"const x = 1.234578;
const y = 1.23e+45;
const z = 1.23e-45;
const a = 1.23e+45;
Object.assign(module.exports, {
x,
y,
z,
a
});"
`;

exports[`numbers transforms hex, octet, binary 1`] = `
"const h = 0x123456789ABCDEF0;
const o = 0o12345670;
const b = 0b01010;
Object.assign(module.exports, {
h,
o,
b
});"
`;

exports[`numbers transforms integers 1`] = `
"const x = 1234578;
const y = 0123;
Object.assign(module.exports, {
x,
y
});"
`;
9 changes: 0 additions & 9 deletions src/transform/__snapshots__/other.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,3 @@ const {
} = nu;
Object.assign(module.exports, {});"
`;
exports[`numbers transforms integers 1`] = `
"const x = 1234578;
const y = 123;
Object.assign(module.exports, {
x,
y
});"
`;
8 changes: 6 additions & 2 deletions src/transform/__snapshots__/regex.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiles regex 1`] = `
"const regex2 = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})mgex3=rx/(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/;
"const regex1 = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/gm;
const regex2 = /(?<year>\\\\d{4})-(?<month>\\\\d{2})-(?<day>\\\\d{2})/;
const regex3 = /.+\\\\/.+/;
Object.assign(module.exports, {
regex2
regex1,
regex2,
regex3
});"
`;
3 changes: 1 addition & 2 deletions src/transform/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
file, program, objectExpression, objectProperty, identifier,
expressionStatement
} from '@babel/types';
import {other_token} from '@fink/prattler/symbols';

import {block_statement} from './block';
import {call, member, ident} from '../types';
Expand All @@ -16,7 +15,7 @@ export const transform_module = (node, ctx)=> {
.map((expr)=> {
const result = block_statement(ctx)(expr);

if (expr?.left?.type === other_token) {
if (expr?.left?.type === 'ident') {
// TODO: wrap with loc?
const id = ident(expr.left.value);

Expand Down
8 changes: 8 additions & 0 deletions src/transform/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {numericLiteral, bigIntLiteral} from '@babel/types';


export const transform_number = ({value})=> (
value.match(/\./)
? numericLiteral(Number(value))
:bigIntLiteral(value)
);
36 changes: 36 additions & 0 deletions src/transform/number.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {fink2js} from '../testing';


describe('numbers', ()=> {
it('transforms integers', ()=> {
expect(
fink2js(`
x = 1234578
y = 0123
`)
).toMatchSnapshot();
});


it('transforms floats', ()=> {
expect(
fink2js(`
x = 1.234578
y = 1.23e45
z = 1.23e-45
a = 1.23e+45
`)
).toMatchSnapshot();
});


it('transforms hex, octet, binary', ()=> {
expect(
fink2js(`
h = 0x123456789ABCDEF0
o = 0o12345670
b = 0b01010
`)
).toMatchSnapshot();
});
});
10 changes: 3 additions & 7 deletions src/transform/other.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/transform/other.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,3 @@ describe('identifiers', ()=> {
).toMatchSnapshot();
});
});


describe('numbers', ()=> {
it('transforms integers', ()=> {
expect(
fink2js(`
x = 1234578
y = 0123
`)
).toMatchSnapshot();
});

// TODO: not yet supported by larix
// it('transforms floats', ()=> {
// expect(
// fink2js(`
// x = 1.234578
// y = 1.23e10
// z = 1.23e-10
// `)
// ).toMatchSnapshot();
// });

// it('transforms hex, octet, binary', ()=> {
// expect(
// fink2js(`
// h = 0xABCDEF
// o = 0o1234567
// b = 0b01010
// `)
// ).toMatchSnapshot();
// });
});
14 changes: 8 additions & 6 deletions src/transform/regex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {fink2js} from '../testing';
test('compiles regex', ()=> {
expect(
fink2js(`
regex2 = rx/
(?<year>\\d{4})- # year part of a date
(?<month>\\d{2})- # month part of a date
(?<day>\\d{2}) # day part of a date
/gm
regex1 = rx/
(?<year>\\d{4})- # year part of a date
(?<month>\\d{2})- # month part of a date
(?<day>\\d{2}) # day part of a date
/gm
regex3 = rx/(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})/
regex2 = rx/(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})/
regex3 = rx/.+\\/.+/
`)
).toMatchSnapshot();
});

0 comments on commit 1e73cd9

Please sign in to comment.