Author: kjs
Date: Fri Feb 22 03:49:03 2008
New Revision: 25983
Modified:
trunk/languages/c99/src/builtins/say.pir
trunk/languages/c99/src/parser/actions.pm
trunk/languages/c99/src/parser/grammar.pg
Log:
[c99] start on actions.pm refactor grammar a bit for this.
Modified: trunk/languages/c99/src/builtins/say.pir
==============================================================================
--- trunk/languages/c99/src/builtins/say.pir (original)
+++ trunk/languages/c99/src/builtins/say.pir Fri Feb 22 03:49:03 2008
@@ -22,6 +22,24 @@
.return ()
.end
+.sub 'printf'
+ .param pmc format
+ .param pmc args :slurpy
+ .local pmc iter
+ iter = new 'Iterator', args
+ iter_loop:
+ unless iter goto iter_end
+ goto iter_loop
+ iter_end:
+.end
+
+.sub 'puts'
+ .param pmc str
+ print str
+ print "\n"
+.end
+
+
# Local Variables:
# mode: pir
Modified: trunk/languages/c99/src/parser/actions.pm
==============================================================================
--- trunk/languages/c99/src/parser/actions.pm (original)
+++ trunk/languages/c99/src/parser/actions.pm Fri Feb 22 03:49:03 2008
@@ -19,34 +19,154 @@
method TOP($/) {
my $past := PAST::Block.new( :blocktype('declaration'), :node( $/ ) );
- for $<statement> {
+ for $<external_declaration> {
$past.push( $( $_ ) );
}
make $past;
}
+method external_declaration($/, $key) {
+ make $( $/{$key} );
+}
-method statement($/) {
- my $past := PAST::Op.new( :name('say'), :pasttype('call'), :node( $/ ) );
- for $<value> {
- $past.push( $( $_ ) );
+method function_definition($/) {
+ my $past := PAST::Block.new( :blocktype('declaration'), :node($/) );
+ my $decl := $( $<declarator> );
+ $past.name( $decl.name() );
+ make $past;
+}
+
+method declaration($/) {
+
+}
+
+method declarator($/) {
+ make $( $<direct_declarator> );
+}
+
+method direct_declarator($/) {
+ make $( $<declarator_prefix> );
+}
+
+method declarator_prefix($/, $key) {
+ make $( $/{$key} );
+}
+
+method statement($/, $key) {
+ make $( $/{$key} );
+}
+
+method expression($/) {
+ make $( $<assignment_expression>[0] );
+}
+
+method expression_statement($/) {
+ if $<expression> {
+ make $( $<expression>[0] );
+ }
+ else {
+ make PAST::Op.new( :inline(' # empty statement'), :node($/) );
+ }
+}
+
+method compound_statement($/) {
+ my $past := PAST::Block.new( :blocktype('immediate'), :node($/) );
+ for $<block_item> {
+ $past.push( $($_) );
}
make $past;
}
+method block_item($/, $key) {
+ make $( $/{$key} );
+}
+
+method constant_expression($/) {
+ make $( $<conditional_expression> );
+}
+
+method assignment_expression($/) {
+ make $( $<conditional_expression> );
+}
-method value($/, $key) {
+method conditional_expression($/) {
+ make $( $<logical_expression> );
+}
+
+method postfix_expression_prefix($/, $key) {
+ make $( $/{$key} );
+}
+
+method postfix_expression_suffix($/, $key) {
+ make $( $/{$key} );
+}
+
+method arguments($/) {
+ if $<argument_expression_list> {
+ make $( $<argument_expression_list>[0] );
+ }
+ else {
+ make PAST::Op.new( :pasttype('call'), :node($/) );
+ }
+}
+
+method argument_expression_list($/) {
+ my $past := PAST::Op.new( :pasttype('call'), :node($/) );
+
+ make $past;
+}
+
+method postfix_expression($/) {
+ my $past := $( $<postfix_expression_prefix> );
+ for $<postfix_expression_suffix> {
+ ## XXX
+ my $args := $( $_ );
+ $past := PAST::Op.new( $past, $args, :pasttype('call'), :node($/) );
+ }
+ make $past;
+}
+
+method primary_expression($/, $key) {
make $( $/{$key} );
}
+method unary_expression($/, $key) {
+ make $( $/{$key} );
+}
method integer($/) {
make PAST::Val.new( :value( ~$/ ), :returns('Integer'), :node($/) );
}
+method c_string_literal($/) {
+ make PAST::Val.new( :value( ~$/ ), :node($/) );
+}
+
+method identifier($/) {
+ make PAST::Var.new( :name( ~$/ ), :scope('package'), :node($/) );
+}
+
+method cast_expression($/) {
+ make $( $<unary_expression> );
+}
+
+method logical_expression($/, $key) {
+ if ($key eq 'end') {
+ make $($<expr>);
+ }
+ else {
+ my $past := PAST::Op.new( :name($<type>),
+ :pasttype($<top><pasttype>),
+ :pirop($<top><pirop>),
+ :lvalue($<top><lvalue>),
+ :node($/)
+ );
+ for @($/) {
+ $past.push( $($_) );
+ }
+ make $past;
+ }
-method quote($/) {
- make PAST::Val.new( :value( $($<string_literal>) ), :node($/) );
}
Modified: trunk/languages/c99/src/parser/grammar.pg
==============================================================================
--- trunk/languages/c99/src/parser/grammar.pg (original)
+++ trunk/languages/c99/src/parser/grammar.pg Fri Feb 22 03:49:03 2008
@@ -18,17 +18,14 @@
token TOP {
^
- <translation_unit>
- [ $ || <.panic: Syntax error> ]
-}
-
-rule translation_unit {
<external_declaration>+
+ [ $ || <.panic: Syntax error> ]
+ {*}
}
rule external_declaration {
- | <declaration>
- | <function_definition>
+ | <declaration> {*} #= declaration
+ | <function_definition> {*} #= function_definition
}
rule function_definition {
@@ -36,6 +33,7 @@
<declarator>
<declaration>*
<compound_statement>
+ {*}
}
@@ -151,14 +149,18 @@
rule declarator {
<pointer>? <direct_declarator>
+ {*}
}
rule direct_declarator {
- [
- | '(' <declarator> ')'
- | <identifier>
- ]
+ <declarator_prefix>
<declarator_suffix>*
+ {*}
+}
+
+rule declarator_prefix {
+ | '(' <declarator> ')' {*} #= declarator
+ | <identifier> {*} #= identifier
}
rule declarator_suffix {
@@ -256,8 +258,8 @@
rule statement {
| <labeled_statement>
- | <compound_statement>
- | <expression_statement>
+ | <compound_statement> {*} #= compound_statement
+ | <expression_statement> {*} #= expression_statement
| <if_statement>
| <switch_statement>
| <while_statement>
@@ -275,15 +277,17 @@
rule compound_statement {
'{' <block_item>* '}'
+ {*}
}
rule block_item {
- | <declaration>
- | <statement>
+ | <declaration> {*} #= declaration
+ | <statement> {*} #= statement
}
rule expression_statement {
<expression>? ';'
+ {*}
}
rule if_statement {
@@ -363,6 +367,7 @@
token identifier {
<!reserved_word>
<identifier_nondigit> [ <identifier_nondigit> | <digit> ]*
+ {*}
}
token identifier_nondigit {
@@ -390,7 +395,9 @@
[ <decimal_constant>
| <octal_constant>
| <hexadecimal_constant>
- ] <integer_suffix>?
+ ]
+ <integer_suffix>?
+ {*}
}
token decimal_constant {
@@ -476,7 +483,10 @@
}
## A.1.6 String literals
-token c_string_literal { [L]? \" <s_char>* \" }
+token c_string_literal {
+ [L]? \" <s_char>* \"
+ {*}
+}
token s_char { <-["\\\n]> | <escape_sequence> }
@@ -489,20 +499,24 @@
rule constant_expression {
<conditional_expression>
+ {*}
}
rule expression {
<assignment_expression> [',' <assignment_expression>]*
+ {*}
}
rule assignment_expression {
[<unary_expression> <assign_op>]* <conditional_expression>
+ {*}
}
rule assign_op { '='|'*='|'/='|'%='|'+='|'-='|'<<='|'>>='|'&='|'^='|'|=' }
rule conditional_expression {
<logical_expression> ['?' <expression> ':' <conditional_expression>]?
+ {*}
}
rule logical_expression is optable { ... }
@@ -539,29 +553,38 @@
is parsed(&cast_expression) { ... }
+rule postfix_expression_prefix {
+ | <primary_expression> {*} #=
primary_expression
+ | '(' <type_name> ')' '{' <initializer_list> [',']? '}' {*} #= type_name
+}
+
rule postfix_expression {
- [
- | <primary_expression>
- | '(' <type_name> ')' '{' <initializer_list> [',']? '}'
- ]
+ <postfix_expression_prefix>
<postfix_expression_suffix>*
+ {*}
}
rule postfix_expression_suffix {
| '[' <expression> ']'
- | '(' <argument_expression_list>? ')'
+ | <arguments> {*} #= arguments
| '.' <identifier>
| '->' <identifier>
| '++'
| '--'
}
+rule arguments {
+ '(' <argument_expression_list>? ')'
+ {*}
+}
+
rule argument_expression_list {
<assignment_expression> [',' <assignment_expression>]*
+ {*}
}
rule unary_expression {
- | <postfix_expression>
+ | <postfix_expression> {*} #= postfix_expression
| ['++'|'--'] <unary_expression>
| <unary_operator> <cast_expression>
| 'sizeof' <unary_expression>
@@ -574,13 +597,14 @@
rule cast_expression {
['(' <type_name> ')']* <unary_expression>
+ {*}
}
rule primary_expression {
- | <identifier>
- | <constant>
- | <c_string_literal>
- | '(' <expression> ')'
+ | <identifier> {*} #= identifier
+ | <constant> {*} #= constant
+ | <c_string_literal> {*} #= c_string_literal
+ | '(' <expression> ')' {*} #= expression
}
token ws {