Author: bernhard
Date: Sun Mar 4 03:08:17 2007
New Revision: 17324
Modified:
trunk/languages/plumhead/src/partridge/Plumhead.pg
trunk/languages/plumhead/src/partridge/PlumheadPAST.tg
trunk/languages/plumhead/t/arithmetics.t
Log:
[Plumhead partridge]
Add support for bitwise ops.
Add some testcases, that add up integers.
Modified: trunk/languages/plumhead/src/partridge/Plumhead.pg
==============================================================================
--- trunk/languages/plumhead/src/partridge/Plumhead.pg (original)
+++ trunk/languages/plumhead/src/partridge/Plumhead.pg Sun Mar 4 03:08:17 2007
@@ -29,6 +29,7 @@
token UNARY_PLUS { \+ }
token ADD_OP { \+ | \- }
token MUL_OP { \* | / | % }
+token BITWISE_OP { \| | \& }
token REL_OP { <'=='> | <'<='> | <'>='> | <'!='> | <'<'> |
<'>'> }
token IF { <'if'> }
@@ -50,12 +51,19 @@
rule else_clause { <?ELSE> \{ <statement>* \} }
token inline_sea { <?CODE_END> <SEA> <?CODE_START> }
-token expression { <DOUBLEQUOTE_STRING> | <SINGLEQUOTE_STRING> |
<adding_expression> }
+token expression { <DOUBLEQUOTE_STRING>
+ | <SINGLEQUOTE_STRING>
+ | <bitwise_expression>
+ }
-# TODO: this gets precedence wrong
-rule adding_expression { <multiplying_expression> <summand>? }
+rule bitwise_expression { <adding_expression> <bitwise_tail>? }
+
+rule bitwise_tail { <BITWISE_OP> <adding_expression> }
+
+# BUG: this gets precedence wrong
+rule adding_expression { <multiplying_expression> <adding_tail>? }
-rule summand { <ADD_OP> <adding_expression> }
+rule adding_tail { <ADD_OP> <adding_expression> }
# TODO: this gets precedence wrong
rule multiplying_expression { <unary_expression> <multiplicand>? }
Modified: trunk/languages/plumhead/src/partridge/PlumheadPAST.tg
==============================================================================
--- trunk/languages/plumhead/src/partridge/PlumheadPAST.tg (original)
+++ trunk/languages/plumhead/src/partridge/PlumheadPAST.tg Sun Mar 4
03:08:17 2007
@@ -173,25 +173,50 @@
goto handled_expression
no_INTEGER:
+ $P0 = node['bitwise_expression']
+ if null $P0 goto no_bitwise_expression
+ .local pmc past_adding, bitwise_tail, bitwise_tail_0,
past_bitwise_tail_0
+ bitwise_tail = $P0['bitwise_tail']
+ if null bitwise_tail goto no_bitwise_tail
+ past_adding = tree.'get'('past', $P0,
'Plumhead::Grammar::expression')
+ bitwise_tail_0 = bitwise_tail[0]
+ .local string op
+ op = bitwise_tail_0['BITWISE_OP']
+ if op != '&' goto not_and
+ op = '+&'
+ not_and:
+ if op != '|' goto not_or
+ op = '+|'
+ not_or:
+ op = 'infix:' . op
+ past_bitwise_tail_0 = tree.'get'('past', bitwise_tail_0,
'Plumhead::Grammar::expression')
+ past = new 'PAST::Op'
+ past.init( past_adding, past_bitwise_tail_0, 'name' => op )
+ goto handled_expression
+ no_bitwise_tail:
+ past = tree.'get'('past', $P0, 'Plumhead::Grammar::expression')
+ goto handled_expression
+ no_bitwise_expression:
+
$P0 = node['adding_expression']
if null $P0 goto no_adding_expression
- .local pmc past_mult, summand, summand_0, past_summand_0
- summand = $P0['summand']
- if null summand goto no_summand
+ .local pmc past_mult, adding_tail, adding_tail_0, past_adding_tail_0
+ adding_tail = $P0['adding_tail']
+ if null adding_tail goto no_adding_tail
past_mult = tree.'get'('past', $P0,
'Plumhead::Grammar::expression')
- summand_0 = summand[0]
+ adding_tail_0 = adding_tail[0]
.local string add_op, pirop
pirop = 'n_add'
- add_op = summand_0['ADD_OP']
+ add_op = adding_tail_0['ADD_OP']
if add_op != '-' goto not_minus
pirop = 'n_sub'
not_minus:
add_op = 'infix:' . add_op
- past_summand_0 = tree.'get'('past', summand_0,
'Plumhead::Grammar::expression')
+ past_adding_tail_0 = tree.'get'('past', adding_tail_0,
'Plumhead::Grammar::expression')
past = new 'PAST::Op'
- past.init( past_mult, past_summand_0, 'name' => add_op, 'pirop' =>
pirop )
+ past.init( past_mult, past_adding_tail_0, 'name' => add_op,
'pirop' => pirop )
goto handled_expression
- no_summand:
+ no_adding_tail:
past = tree.'get'('past', $P0, 'Plumhead::Grammar::expression')
goto handled_expression
no_adding_expression:
Modified: trunk/languages/plumhead/t/arithmetics.t
==============================================================================
--- trunk/languages/plumhead/t/arithmetics.t (original)
+++ trunk/languages/plumhead/t/arithmetics.t Sun Mar 4 03:08:17 2007
@@ -68,6 +68,12 @@
[ '1 + 2', '3', 'two summands', ],
[ '1 + 2 + 3', '6', 'three summands', ],
[ '1 + 0 + 3', '4', 'three summands including 0', ],
+ [ '1 + 2 + 3 + 4 + 5', '15', '5 summands', ],
+ [ '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8', '36', '8 summands', ],
+ [ '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10', '55', '10 summands', ],
+ [ '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11', '66', '11 summands', ],
+ [ '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12', '78', '12 summands',
],
+ [ '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13', '91', '13
summands', ],
[ '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14', '105',
'Gauss was right', ],
[ '-1 + 10', '9', 'negative int in expression', ],
[ '2 - 1', '1', 'subtraction' ],