[stupid-crypto] 3 new revisions pushed by ben@links.org on 2010-03-14 00:18 GMT

1 view
Skip to first unread message

stupid...@googlecode.com

unread,
Mar 13, 2010, 7:18:38 PM3/13/10
to stupi...@googlegroups.com
3 new revisions:

Revision: ad956a9db1
Author: Ben Laurie <be...@google.com>
Date: Mon Mar 8 21:31:36 2010
Log: Appears to be a working parse. No code emission yet. And I'm sure...
http://code.google.com/p/stupid-crypto/source/detail?r=ad956a9db1

Revision: 0ea89b79c3
Author: Ben Laurie <be...@google.com>
Date: Sat Mar 13 16:10:17 2010
Log: Make structs work.
http://code.google.com/p/stupid-crypto/source/detail?r=0ea89b79c3

Revision: 394bff2d36
Author: Ben Laurie <be...@google.com>
Date: Sat Mar 13 16:12:21 2010
Log: Merge.
http://code.google.com/p/stupid-crypto/source/detail?r=394bff2d36

==============================================================================
Revision: ad956a9db1
Author: Ben Laurie <be...@google.com>
Date: Mon Mar 8 21:31:36 2010
Log: Appears to be a working parse. No code emission yet. And I'm sure
there's now dead code.
http://code.google.com/p/stupid-crypto/source/detail?r=ad956a9db1

Modified:
/src/grammar.y
/src/stupid.pl
/test/build-C.sh

=======================================
--- /src/grammar.y Sat Feb 13 04:20:10 2010
+++ /src/grammar.y Mon Mar 8 21:31:36 2010
@@ -2,24 +2,46 @@

%%

-prog : functions
+prog : toplevel_list
{ $_[1]; }
;

-functions : functions function
- { $_[1]->appendFunction($_[2]); $_[1]; }
+toplevel_list : toplevel_list toplevel
+ { $_[1]->appendTopLevel($_[2]); $_[1]; }
+ | toplevel
+ { my $t = new Stupid::TopLevelList();
+ $t->appendTopLevel($_[1]);
+ $t; }
+ ;
+
+toplevel : comment
| function
- { my $t1 = new Stupid::FunctionList();
- $t1->appendFunction($_[1]);
- $t1; }
+ | struct_decl
;

+struct_decl : 'struct' WORD '(' abstract_decl_list ')' ';'
+ { new Stupid::Type::Struct($_[2], $_[4]); }
+ ;
+
+abstract_decl_list : abstract_decl_list ',' abstract_decl
+ { $_[1]->appendAbstractDecl($_[3]); $_[1]; }
+ | abstract_decl
+ { my $t = new Stupid::AbstractDeclList();
+ $t->appendAbstractDecl($_[1]);
+ $t; }
+ ;
+
+abstract_decl : type vardecl
+ { new Stupid::AbstractDeclare($_[1], $_[2]); }
+ | 'array' '(' type ',' VALUE ')'
+ { new Stupid::AbstractDeclare(
+ new Stupid::Type::Array($_[3], $_[5])); }
+ ;
+
function : 'function' '(' arglist ')' WORD '(' arglist ')'
'{' statements '}'
{ $_[3]->markAsReturn();
new Stupid::Function($_[5], $_[3], $_[7], $_[10]); }
- | comment ';'
- { $_[1]; }
;

arglist : arglist ',' arg
@@ -137,6 +159,8 @@
{ new Stupid::Declare($::Context,
new Stupid::Variable(new Stupid::Type::Array($_[3], $_[5]),
$_[7]), $_[9]); }
+ | 'struct' WORD vardecl '=' arrayval
+ { new Stupid::Declare($::Context, new Stupid::Variable(new
Stupid::Type::StructInstance($_[2]), $_[3]), $_[5]); }
;

type : 'uint32'
=======================================
--- /src/stupid.pl Sun Mar 7 15:55:58 2010
+++ /src/stupid.pl Mon Mar 8 21:31:36 2010
@@ -30,7 +30,7 @@
yyerror => \&yyerror,
yydebug => $debug ? 6 : 0);

-use Data::Dumper; print STDERR Data::Dumper->Dump([\$ptree]) if $debug;
+use Data::Dumper; Data::Dumper->Indent(1); print STDERR
Data::Dumper->Dump([\$ptree]) if $debug;

#$ptree->value();
#$::Context->dumpSymbols();
@@ -47,7 +47,7 @@

sub initLexer {
@keywords = qw(array uint8 uint32 ostream
- function if else while
+ function struct if else while
and8 and32 band bor eq32 ge8 le8 lshift32 lshift8 mask32to8 minus8 minus32
mod8 mod32 ne32 not32 not8 or8 plus8 plus32 rrotate32 rshift32 widen8to32
xor32);
@@ -243,7 +243,9 @@
return $self;
}

-package Stupid::FunctionList;
+package Stupid::TopLevelList;
+
+# FIXME: topl-level classes should inherit from a TopLevel class.

use strict;

@@ -253,16 +255,90 @@
my $self = {};
bless $self, $class;

- $self->{functions} = [];
+ $self->{topLevels} = [];

return $self;
}

-sub appendFunction {
+sub appendTopLevel {
my $self = shift;
- my $fn = shift;
-
- push @{$self->{functions}}, $fn;
+ my $tl = shift;
+
+ push @{$self->{functions}}, $tl;
+}
+
+package Stupid::Type::Struct;
+
+use strict;
+
+sub new {
+ my $class = shift;
+ my $name = shift;
+ my $decls = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{name} = $name;
+ $self->{decls} = $decls;
+
+ return $self;
+}
+
+package Stupid::Type::StructInstance;
+
+use strict;
+
+sub new {
+ my $class = shift;
+ my $name = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{name} = $name;
+
+ return $self;
+}
+
+package Stupid::AbstractDeclare;
+
+use strict;
+
+sub new {
+ my $class = shift;
+ my $type = shift;
+ my $decl = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{type} = $type;
+ $self->{decl} = $decl;
+
+ return $self;
+}
+
+package Stupid::AbstractDeclList;
+
+use strict;
+
+sub new {
+ my $class = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{decls} = [];
+
+ return $self;
+}
+
+sub appendAbstractDecl {
+ my $self = shift;
+ my $decl = shift;
+
+ push @{$self->{decls}}, $decl;
}

package Stupid::ExprList;
=======================================
--- /test/build-C.sh Tue Mar 2 07:29:31 2010
+++ /test/build-C.sh Mon Mar 8 21:31:36 2010
@@ -5,5 +5,5 @@

TARGET=$1

-PERLLIB=../src ../src/stupid.pl --language=C ${TARGET}.stupid >
generated/C/${TARGET}.c
+PERLLIB=../src ../src/stupid.pl --language=C ${TARGET}.stupid -debug >
generated/C/${TARGET}.c
gcc -Wall -Werror -o generated/C/${TARGET} test-wrapper.c
generated/C/${TARGET}.c

==============================================================================
Revision: 0ea89b79c3
Author: Ben Laurie <be...@google.com>
Date: Sat Mar 13 16:10:17 2010
Log: Make structs work.
http://code.google.com/p/stupid-crypto/source/detail?r=0ea89b79c3

Modified:
/src/Stupid/C.pm
/src/grammar.y
/src/stupid.pl
/test/build-C.sh

=======================================
--- /src/Stupid/C.pm Sun Mar 7 15:55:58 2010
+++ /src/Stupid/C.pm Sat Mar 13 16:10:17 2010
@@ -25,10 +25,10 @@
$self->{tree}->emitCode();
}

-sub Stupid::FunctionList::emitCode {
+sub Stupid::TopLevelList::emitCode {
my $self = shift;

- for my $f (@{$self->{functions}}) {
+ for my $f (@{$self->{topLevels}}) {
$f->emitCode();
}
}
@@ -101,18 +101,35 @@
print ";\n";
}

-sub Stupid::MemberCall::emitCode {
+sub Stupid::FunctionCall::emitCode {
my $self = shift;

- $self->{owner}->emitCode();
- print "->$self->{member}(";
- # something of a hack at this stage
- $self->{owner}->emitCode();
- print '->info, ';
- # end of hack
+ $self->{function}->emitCode();
+ print '(';
+ $self->{function}->maybeAddSelf();
$self->{args}->emitCode();
print ");\n";
}
+
+sub Stupid::MemberRef::emitCode {
+ my $self = shift;
+
+ $self->{owner}->emitCode();
+ $self->{owner}->emitMemberRef($self->{member});
+}
+
+sub Stupid::MemberRef::maybeAddSelf {
+ my $self = shift;
+
+ $self->{owner}->maybeAddSelf();
+}
+
+sub Stupid::MemberRef::emitLValue {
+ my $self = shift;
+
+ $self->{owner}->emitCode();
+ print ".$self->{member}";
+}

sub Stupid::ExprList::emitCode {
my $self = shift;
@@ -193,6 +210,14 @@

print $self->{name};
}
+
+sub Stupid::Variable::emitMemberRef {
+ my $self = shift;
+ my $name = shift;
+
+ print $self->{isReturn} ? '->' : '.';
+ print $name;
+}

sub Stupid::Variable::emitLValue {
my $self = shift;
@@ -200,6 +225,43 @@
$self->{type}->dereference() if $self->{isReturn};
print $self->{name};
}
+
+sub Stupid::Variable::maybeAddSelf {
+ my $self = shift;
+
+ print "$self->{name}, " if $self->{type}->needsSelf();
+}
+
+sub Stupid::Type::Struct::emitCode {
+ my $self = shift;
+
+ print "struct $self->{name} {\n";
+ $self->{decls}->emitCode();
+ print "};\n";
+}
+
+sub Stupid::Type::StructInstance::emitDeclaration {
+ my $self = shift;
+ my $name = shift;
+
+ print "struct $self->{name} $name";
+}
+
+sub Stupid::AbstractDeclList::emitCode {
+ my $self = shift;
+
+ foreach my $decl (@{$self->{decls}}) {
+ $decl->emitCode();
+ }
+}
+
+sub Stupid::AbstractDeclare::emitCode {
+ my $self = shift;
+
+ print ' ';
+ $self->{type}->emitDeclaration($self->{name});
+ print ";\n";
+}

sub Stupid::Type::UInt32::dereference {
my $self = shift;
@@ -278,6 +340,10 @@
sub Stupid::Type::OStream::emitArg {
croak "ostreams must be outputs";
}
+
+sub Stupid::Type::OStream::needsSelf {
+ return 1;
+}

sub Stupid::Type::Array::emitReturnDecl {
my $self = shift;
=======================================
--- /src/grammar.y Mon Mar 8 21:31:36 2010
+++ /src/grammar.y Sat Mar 13 16:10:17 2010
@@ -14,7 +14,7 @@
$t; }
;

-toplevel : comment
+toplevel : comment ';'
| function
| struct_decl
;
@@ -77,12 +77,12 @@
{ $_[1]; }
| var '=' expr ';'
{ new Stupid::Statement(new Stupid::Set($_[1], $_[3])); }
- | expr '.' WORD '(' exprlist ')' ';'
- { new Stupid::MemberCall($_[1], $_[3], $_[5]); }
| 'if' '(' expr ')' '{' statements '}' 'else' '{' statements '}'
{ new Stupid::If($_[3], $_[6], $_[10]); }
| 'while' '(' expr ')' '{' statements '}'
{ new Stupid::While($_[3], $_[6]); }
+ | call ';'
+ { $_[1]; }
;

expr : expr 'and32' expr
@@ -150,6 +150,13 @@
{ $::Context->findSymbol($_[1]); }
| var '[' expr ']'
{ new Stupid::ArrayRef($_[1], $_[3]); }
+ | expr '.' WORD
+ { new Stupid::MemberRef($_[1], $_[3]); }
+ | call
+ ;
+
+call: | expr '(' exprlist ')'
+ { new Stupid::FunctionCall($_[1], $_[3], $_[5]); }
;

decl : type vardecl '=' VALUE
=======================================
--- /src/stupid.pl Mon Mar 8 21:31:36 2010
+++ /src/stupid.pl Sat Mar 13 16:10:17 2010
@@ -264,7 +264,7 @@
my $self = shift;
my $tl = shift;

- push @{$self->{functions}}, $tl;
+ push @{$self->{topLevels}}, $tl;
}

package Stupid::Type::Struct;
@@ -308,13 +308,13 @@
sub new {
my $class = shift;
my $type = shift;
- my $decl = shift;
+ my $name = shift;

my $self = {};
bless $self, $class;

$self->{type} = $type;
- $self->{decl} = $decl;
+ $self->{name} = $name;

return $self;
}
@@ -363,7 +363,25 @@
push @{$self->{expressions}}, $expr;
}

-package Stupid::MemberCall;
+package Stupid::FunctionCall;
+
+use strict;
+
+sub new {
+ my $class = shift;
+ my $function = shift;
+ my $args = shift;
+
+ my $self = {};
+ bless $self, $class;
+
+ $self->{function} = $function;
+ $self->{args} = $args;
+
+ return $self;
+}
+
+package Stupid::MemberRef;

use strict;

@@ -371,14 +389,12 @@
my $class = shift;
my $owner = shift;
my $member = shift;
- my $args = shift;

my $self = {};
bless $self, $class;

$self->{owner} = $owner;
$self->{member} = $member;
- $self->{args} = $args;

return $self;
}
=======================================
--- /test/build-C.sh Mon Mar 8 21:31:36 2010
+++ /test/build-C.sh Sat Mar 13 16:10:17 2010
@@ -5,5 +5,5 @@

TARGET=$1

-PERLLIB=../src ../src/stupid.pl --language=C ${TARGET}.stupid -debug >
generated/C/${TARGET}.c
+PERLLIB=../src ../src/stupid.pl --language=C ${TARGET}.stupid >
generated/C/${TARGET}.c
gcc -Wall -Werror -o generated/C/${TARGET} test-wrapper.c
generated/C/${TARGET}.c

==============================================================================
Revision: 394bff2d36
Author: Ben Laurie <be...@google.com>
Date: Sat Mar 13 16:12:21 2010
Log: Merge.
http://code.google.com/p/stupid-crypto/source/detail?r=394bff2d36


Reply all
Reply to author
Forward
0 new messages