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
24 changes: 17 additions & 7 deletions af.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const af = {
type: 'NUM',
value: Number(n),
}),
operator: op => ({
type: 'OPERATOR',
value: op,
}),
declareAssign: (id, exp) => ({
type: 'DEC_ASSIGN',
id,
Expand All @@ -27,26 +31,32 @@ const af = {
type: 'LAMBDA',
arguments,
statements
})
}),
exp: (exp, left, right) => ({
type: "EXP",
value: exp,
Copy link
Contributor Author

@tigershen23 tigershen23 Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richardartoul Can the value ever be something besides an operator with the way you wrote https://github.com/Bradfield/languages-2017-01/pull/2/files#diff-fe30eed34176a05cc930283036bce555R39? If we don't have a valid value that isn't an operator, what do you think about calling this key op to avoid ambiguity?

left: left,
right: right,
}),
}

/*
{ type: program,
statements: [
{
{
type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'foobar' },
exp: { type: 'NUM', value: 23 }
exp: { type: 'NUM', value: 23 }
},
{
{
type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'foo' },
exp: { type: 'STRING', value: 'Hello World' }
exp: { type: 'STRING', value: 'Hello World' }
},
{
{
type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'bar' },
exp: { type: 'NUM', value: 10 }
exp: { type: 'NUM', value: 10 }
}
]
}
Expand Down
11 changes: 10 additions & 1 deletion examples/1.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
let foo = "Hello";
module.exports = {
testCase: `let foo = "Hello";`,
expectedOutput: [ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'foo' },
exp:
{ type: 'EXP',
value: { type: 'STRING', value: 'Hello' },
left: null,
right: null } } ]
}
18 changes: 17 additions & 1 deletion examples/2.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
let foo = "Hello"; let bar = "World";
module.exports = {
testCase: `let foo = "Hello"; let bar = "World";`,
expectedOutput: [ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'foo' },
exp:
{ type: 'EXP',
value: { type: 'STRING', value: 'Hello' },
left: null,
right: null } },
{ type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'bar' },
exp:
{ type: 'EXP',
value: { type: 'STRING', value: 'World' },
left: null,
right: null } } ]
}
19 changes: 18 additions & 1 deletion examples/3.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
let foo = "Hello"; let bar = "World"; a();
module.exports = {
testCase: `let foo = "Hello"; let bar = "World"; a();`,
expectedOutput: [ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'foo' },
exp:
{ type: 'EXP',
value: { type: 'STRING', value: 'Hello' },
left: null,
right: null } },
{ type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'bar' },
exp:
{ type: 'EXP',
value: { type: 'STRING', value: 'World' },
left: null,
right: null } },
{ type: 'FUNC_CALL', name: 'a', arguments: [] } ]
}
15 changes: 14 additions & 1 deletion examples/4.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
a(36, "bob");
module.exports = {
testCase: `a(36, "bob");`,
expectedOutput: [ { type: 'FUNC_CALL',
name: 'a',
arguments:
[ { type: 'EXP',
value: { type: 'NUM', value: 36 },
left: null,
right: null },
{ type: 'EXP',
value: { type: 'STRING', value: 'bob' },
left: null,
right: null } ] } ]
}
32 changes: 32 additions & 0 deletions examples/5.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
let foo = (a, b) => {
let bar = "hey";
};

module.exports = {
testCase: `
let foo = (a, b) => {
let bar = "hey";
};`,
expectedOutput: [ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'foo' },
exp:
{ type: 'EXP',
value:
{ type: 'LAMBDA',
arguments:
[ { type: 'EXP',
value: { type: 'ID', value: 'a' },
left: null,
right: null },
{ type: 'EXP',
value: { type: 'ID', value: 'b' },
left: null,
right: null } ],
statements:
[ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'bar' },
exp:
{ type: 'EXP',
value: { type: 'STRING', value: 'hey' },
left: null,
right: null } } ] },
left: null,
right: null } } ]
}
51 changes: 51 additions & 0 deletions examples/6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
[ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'test' },
exp:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'PLUS' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 1 },
left: null,
right: null },
right:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'MULTIPLY' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 3 },
left: null,
right: null },
right:
{ type: 'EXP',
value: { type: 'NUM', value: 2 },
left: null,
right: null } } } } ]
*/
module.exports = {
testCase: "let test = (1 + 3 * (2));",
expectedOutput: [ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'test' },
exp:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'PLUS' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 1 },
left: null,
right: null },
right:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'MULTIPLY' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 3 },
left: null,
right: null },
right:
{ type: 'EXP',
value: { type: 'NUM', value: 2 },
left: null,
right: null } } } } ]
}
41 changes: 41 additions & 0 deletions examples/7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = {
testCase: `let test = (1 + 3 * (2) - someFunc(1));`,
expectedOutput: [ { type: 'DEC_ASSIGN',
id: { type: 'ID', value: 'test' },
exp:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'PLUS' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 1 },
left: null,
right: null },
right:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'MULTIPLY' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 3 },
left: null,
right: null },
right:
{ type: 'EXP',
value: { type: 'OPERATOR', value: 'MINUS' },
left:
{ type: 'EXP',
value: { type: 'NUM', value: 2 },
left: null,
right: null },
right:
{ type: 'EXP',
value:
{ type: 'FUNC_CALL',
name: 'someFunc',
arguments:
[ { type: 'EXP',
value: { type: 'NUM', value: 1 },
left: null,
right: null } ] },
left: null,
right: null } } } } } ]
}
32 changes: 24 additions & 8 deletions lang.g
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,47 @@ program
;

statements
: statement -> [$1]
: statement -> [$1]
| statements statement -> $1.concat($2)
;
;

statement
: declaration_assignment SEMICOLON
| id OPAREN arguments CPAREN SEMICOLON -> af.funcCall($1, $3)
| function_call SEMICOLON -> $1
;

arguments
: exp -> [$1]
: -> []
| exp -> [$1]
| arguments COMMA exp -> $1.concat($3)
;

declaration_assignment
: LET id ASSIGN exp -> af.declareAssign($2, $4)
;

function_call
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

: id OPAREN arguments CPAREN -> af.funcCall($1, $3)
;

exp
: num
| string
| lambda
| id
: num -> af.exp($1, null, null)
| string -> af.exp($1, null, null)
| lambda -> af.exp($1, null, null)
| id -> af.exp($1, null, null)
| function_call -> af.exp($1, null, null)
| exp operator exp -> af.exp($2, $1, $3)
| OPAREN exp CPAREN -> $2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

;

operator
: MINUS -> af.operator("MINUS")
| PLUS -> af.operator("PLUS")
| MULTIPLY -> af.operator("MULTIPLY")
| DIVIDE -> af.operator("DIVIDE")
;


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: xtra whitespace

lambda
: OPAREN arguments CPAREN ROCKET OCURLY statements CCURLY -> af.lambda($2, $6)
;
Expand Down
4 changes: 4 additions & 0 deletions lang.l
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ scomment-text {scomment-char}*\n
',' return 'COMMA'
'=' return 'ASSIGN'
';' return 'SEMICOLON'
'+' return 'PLUS'
'-' return 'MINUS'
'*' return 'MULTIPLY'
'/' return 'DIVIDE'
if return 'IF'
else return 'ELSE'
while return 'WHILE'
Expand Down
Loading