Skip to content

Commit

Permalink
Implement bool literals (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
izqui authored and sohkai committed Oct 26, 2018
1 parent 1dcc704 commit 830afbc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
38 changes: 25 additions & 13 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions src/evaluator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ class Evaluator {
return new TypedValue(`bytes${length}`, node.value)
}

if (node.type === 'BoolLiteral') {
return new TypedValue('bool', node.value === 'true')
}

if (node.type === 'BinaryExpression') {
const left = await this.evaluateNode(node.left)
const right = await this.evaluateNode(node.right)
Expand Down
5 changes: 3 additions & 2 deletions src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,12 @@ class Parser {
* @return {Node}
*/
primary (astBody) {
if (this.matches('NUMBER', 'STRING', 'HEXADECIMAL')) {
if (this.matches('NUMBER', 'STRING', 'HEXADECIMAL', 'BOOLEAN')) {
let type = {
NUMBER: 'NumberLiteral',
STRING: 'StringLiteral',
HEXADECIMAL: 'BytesLiteral'
HEXADECIMAL: 'BytesLiteral',
BOOLEAN: 'BoolLiteral'
}[this.previous().type]

return {
Expand Down
5 changes: 5 additions & 0 deletions src/scanner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ class Scanner {
identifier += this.consume()
}

if (identifier === 'true' || identifier === 'false') {
this.emitToken('BOOLEAN', identifier)
break
}

if (types.isType(identifier)) {
this.emitToken('TYPE', identifier)
} else {
Expand Down
9 changes: 7 additions & 2 deletions test/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ const helperCases = [
}, 'helper hi hi '],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 5)` ANT',
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), balance: int('647413054595780000000000'), false: bool(false) } // TODO: make false a special identifier
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), balance: int('647413054595780000000000')}
}, 'Balance: 647413.05459 ANT'],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 5)` ANT (non-checksummed)',
bindings: { token: address('0x960b236a07cf122663c4303350609a66a7b288c0'), balance: int('647413054595780000000000'), false: bool(false) } // TODO: make false a special identifier
bindings: { token: address('0x960b236a07cf122663c4303350609a66a7b288c0'), balance: int('647413054595780000000000')}
}, 'Balance: 647413.05459 ANT (non-checksummed)'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
Expand Down Expand Up @@ -207,6 +207,11 @@ const cases = [
}
}, 'This will default to 1: 1'],

[{
source: 'True is not `false ? true : false`',
bindings: {},
}, 'True is not false'],

// External calls
[{
source: 'Allocate `amount token.symbol(): string`.',
Expand Down
24 changes: 24 additions & 0 deletions test/scanner/booleans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const test = require('ava')
const { scan } = require('../../src/scanner')

test('Scanner: Booleans', async (t) => {
t.plan(2)

t.deepEqual(
await scan('`true`'),
[
{ type: 'TICK' },
{ type: 'BOOLEAN', value: 'true' },
{ type: 'TICK' }
]
)

t.deepEqual(
await scan('`false`'),
[
{ type: 'TICK' },
{ type: 'BOOLEAN', value: 'false' },
{ type: 'TICK' }
]
)
})

0 comments on commit 830afbc

Please sign in to comment.