Skip to content

Commit

Permalink
Add Some Features.
Browse files Browse the repository at this point in the history
    1) support 32bit architecture
    2) support package system
    3) support use/use base
    4) support global variable
    5) add some test codes
  • Loading branch information
goccy committed Aug 29, 2013
1 parent 34ec3f2 commit 52b2c68
Show file tree
Hide file tree
Showing 76 changed files with 265,175 additions and 2,499 deletions.
9 changes: 9 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ makemaker_args->{CC} = 'g++';
makemaker_args->{LD} = 'g++';
my @ignore_warnings_options = qw(missing-field-initializers unused-variable);
makemaker_args->{CCFLAGS} .= join('', map { ' -Wno-' . $_ } @ignore_warnings_options);
chomp(my $bin_dir = `llvm-config --bindir`);
my $clang = "$bin_dir/clang";
die "could not find clang" unless ($clang);

my $llvm_lib_dir = 'lib/Compiler/CodeGenerator/LLVM';
`$clang -emit-llvm -S -o $llvm_lib_dir/runtime_api.ll gen/runtime_api.c`;
`$clang -emit-llvm -arch i386 -S -o $llvm_lib_dir/runtime_api_32.ll gen/runtime_api_32.c`;
`cp gen/*.h $llvm_lib_dir`;

if (DEBUG) {
makemaker_args->{OPTIMIZE} = '-O0';
chomp(my $llvm_cflags = `llvm-config --cxxflags`);
Expand Down
5 changes: 4 additions & 1 deletion example/array.pl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
say $a[0];
$a[0] = 3;
say @a;
my $b = 0;
say $a[$b];
$a[$b + 1] = 2;
say @a;
#my @c = map { $_ * 2; } @a;
#say @c;
32 changes: 32 additions & 0 deletions example/assign.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use strict;
use warnings;
use Compiler::Lexer;
use Compiler::Parser;
use Compiler::Parser::AST::Renderer;
use Compiler::CodeGenerator::LLVM;

my $code = do { local $/; <DATA> };
my $tokens = Compiler::Lexer->new('')->tokenize($code);
my $parser = Compiler::Parser->new();
my $ast = $parser->parse($tokens);
Compiler::Parser::AST::Renderer->new->render($ast);
my $generator = Compiler::CodeGenerator::LLVM->new();
my $llvm_ir = $generator->generate($ast);
open my $fh, '>', 'assign.ll';
print $fh $llvm_ir;
close $fh;

$generator = Compiler::CodeGenerator::LLVM->new();
$generator->debug_run($ast);

__DATA__
my $a = 10;
$a += 2;
say $a;
$a -= 2;
say $a;
$a *= 2;
say $a;
$a /= 2;
say $a;
4 changes: 4 additions & 0 deletions example/hash_ref.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
$generator->debug_run($ast);

__DATA__
<<<<<<< HEAD
my $a = { a => 1, b => { d => 2 }, c => 2 };
=======
my $a = { a => 1, b => { d => 8 }, c => 2 };
>>>>>>> origin/dev
say $a;
say %$a;
Expand Down
1 change: 0 additions & 1 deletion example/loop.pl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
my @a = (1, 2, 3, 4);
say @a;
say "=============";
foreach my $itr (@a) {
say $itr;
Expand Down
1 change: 1 addition & 0 deletions example/operator.pl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
$generator->debug_run($ast);

__DATA__
say undef;
say 1;
say 2;
say 1 + 2 == 3;
Expand Down
51 changes: 51 additions & 0 deletions example/package.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use strict;
use warnings;
use Compiler::Lexer;
use Compiler::Parser;
use Compiler::Parser::AST::Renderer;
use Compiler::CodeGenerator::LLVM;

my $code = do { local $/; <DATA> };
my $tokens = Compiler::Lexer->new('')->tokenize($code);
my $parser = Compiler::Parser->new();
my $ast = $parser->parse($tokens);
Compiler::Parser::AST::Renderer->new->render($ast);
my $generator = Compiler::CodeGenerator::LLVM->new();
my $llvm_ir = $generator->generate($ast);
open my $fh, '>', 'package.ll';
print $fh $llvm_ir;
close $fh;
print "generated\n";
$generator = Compiler::CodeGenerator::LLVM->new();
$generator->debug_run($ast);

__DATA__
package Person;
sub new {
my ($class, $name, $age) = @_;
my $self = {
age => $age,
name => $name
};
return bless($self, $class);
}
sub get_name {
my $self = shift;
$self->{name};
}
sub get_age {
my $self = shift;
$self->{age};
}
package main;
my $person = Person->new("goccy", 26);
say $person;
say $person->get_name();
say $person->get_age;
68 changes: 34 additions & 34 deletions example/sub.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,48 @@ sub f {
my $a = $_[0];
my $b = $_[1];
my $c = $a + $b;
say $a;
say $b;
say "$a : ", $a;
say "$b : ", $b;
# Object op Object
say $a + $b;
say $a - $b;
say $a * $b;
say $a / $b;
say $a < $b;
say $a > $b;
say $a == $b;
say $a != $b;
say "$a + $b : ", $a + $b;
say "$a - $b : ", $a - $b;
say "$a * $b : ", $a * $b;
say "$a / $b : ", $a / $b;
say "$a < $b : ", $a < $b;
say "$a > $b : ", $a > $b;
say "$a == $b : ", $a == $b;
say "$a != $b : ", $a != $b;
# Object op Int
say $a + 2;
say $b - 1;
say $b * 2;
say $b / 1;
say $a < 1;
say $a > 1;
say $a == 1;
say $a != 1;
say "$a + 2 : ", $a + 2;
say "$b - 1 : ", $b - 1;
say "$b * 2 : ", $b * 2;
say "$b / 1 : ", $b / 1;
say "$a < 1 : ", $a < 1;
say "$a > 1 : ", $a > 1;
say "$a == 1 : ", $a == 1;
say "$a != 1 : ", $a != 1;
# Int op Object
say 2 + $b;
say 1 - $b;
say 2 * $b;
say 1 / $b;
say 1 < $a;
say 1 > $a;
say 1 == $a;
say 1 != $a;
say "2 + $b : ", 2 + $b;
say "1 - $b : ", 1 - $b;
say "2 * $b : ", 2 * $b;
say "1 / $b : ", 1 / $b;
say "0 < $a : ", 0 < $a;
say "1 > $a : ", 1 > $a;
say "1 == $a : ", 1 == $a;
say "1 != $a : ", 1 != $a;
# Object op Double
say $a < 1.2;
say $a > 1.2;
say $a + 2.1;
say $b - 1.2;
say $b * 1.2;
say $b / 1.2;
say $a == 1.2;
say $a != 1.2;
say "$a < 1.2 : ", $a < 1.2;
say "$a > 1.2 : ", $a > 1.2;
say "$a + 2.1 : ", $a + 2.1;
say "$b - 1.2 : ", $b - 1.2;
say "$b * 1.2 : ", $b * 1.2;
say "$b / 1.2 : ", $b / 1.2;
say "$a == 1.2 : ", $a == 1.2;
say "$a != 1.2 : ", $a != 1.2;
# Double op Object
say 2.1 + $b;
Expand Down
Loading

0 comments on commit 52b2c68

Please sign in to comment.