Permalink
Browse files

ready, go!

  • Loading branch information...
0 parents commit 664e9f9ad1c8df8365c70e3910419031cdba1672 @douglascamata committed Mar 8, 2012
Showing with 445 additions and 0 deletions.
  1. +13 −0 codigo.pasc
  2. +4 −0 exemplo_de_uso.rb
  3. +61 −0 gerador.rb
  4. +169 −0 gramatica.tt
  5. +167 −0 nos.rb
  6. +31 −0 pasc.rb
@@ -0,0 +1,13 @@
+y = 1;
+x = 1;
+if(x == 1)
+y = 2;
+endif
+
+while(y == 2)
+y = y + 1;
+endwhile
+
+for(y = 1; y < 10; y = y + 1)
+x = x + 1;
+endfor
@@ -0,0 +1,4 @@
+require './pasc'
+c = PascCompiler.new
+codigo = File.new('codigo.pasc').read
+c.compile_and_run codigo
@@ -0,0 +1,61 @@
+['core', 'core/builder', 'execution_engine', 'transforms/scalar'].each do |filename|
+ require "llvm/#{filename}"
+end
+
+class Gerador
+
+ def initialize(syntax_tree)
+ @tree = syntax_tree
+ end
+
+ def compile
+ LLVM.init_x86
+ @module = LLVM::Module.new("pasc")
+ @builder = LLVM::Builder.new
+ @function = @module.functions.add("main", [], LLVM::Int)
+ @basic_block = @function.basic_blocks.append
+ @builder.position_at_end(@basic_block)
+ @locals = Hash.new
+
+ generate_code
+
+ @builder.ret LLVM::Int(0)
+
+ p
+ puts "-----------------------------------------------------------------"
+ puts "Generating LLVM code..."
+ puts "-----------------------------------------------------------------"
+ @module.dump
+ @module.verify
+ p
+ puts "-----------------------------------------------------------------"
+ puts "Writting LLVM bitcode..."
+ puts "-----------------------------------------------------------------"
+ @module.write_bitcode('./codigo.ir')
+ puts "Compiling LLVM bitcode to unoptimized machine code..."
+ puts "-----------------------------------------------------------------"
+ system 'llc codigo.ir -O0 -o codigo.s'
+ system 'cat codigo.s'
+ puts "-----------------------------------------------------------------"
+ puts "Optimizing the machine code..."
+ puts "-----------------------------------------------------------------"
+ system 'llc codigo.ir -O2 -o codigo_otimizado.s'
+ system 'cat codigo_otimizado.s'
+ puts "-----------------------------------------------------------------"
+ system 'rm -rf codigo.ir'
+ self
+ end
+
+ def run
+ # Execution objects.
+ @engine = LLVM::JITCompiler.new(@module)
+ @engine.run_function(@function)
+ end
+
+ private
+
+ def generate_code
+ @tree.generate_code @function, @locals, @builder, @basic_block
+ end
+
+end
@@ -0,0 +1,169 @@
+grammar Pasc
+
+ ##################
+ # Program structure
+
+ rule block
+ (comment / statement)+ <Block>
+ end
+
+ rule statement
+ space? ( assignment / if / while / for) ';'? space? linebreak? <Statement>
+ end
+
+ ##################
+ # Literals
+
+ rule integer
+ ('+' / '-')? [0-9]+ <IntegerLiteral>
+ end
+
+ rule float
+ ('+' / '-')? [0-9]+ '.' [0-9]+ <FloatLiteral>
+ end
+
+ rule string
+ '"' ([^"\\] / "\\" . )* '"' <StringLiteral>
+ end
+
+ rule identifier
+ '$'? [a-zA-Z] [a-zA-Z0-9_]* ('?' / '!')? <Identifier>
+ end
+
+ ##################
+ # Assignment
+
+ rule assignment
+ identifier space? assignment_operator space? ( expression ) <Assignment>
+ end
+
+ ##################
+ # Flow control
+
+ rule if
+ 'if(' space? comparative space? ')' space
+ block
+ space? 'endif' space? <If>
+ end
+
+ rule while
+ 'while(' space? comparative space? ')' space
+ block
+ space? 'endwhile' space? <While>
+ end
+
+ rule for
+ 'for(' space? assignment ';' space? comparative ';' space? assignment space? ')'
+ block
+ space? 'endfor' space? <For>
+ end
+
+ ##################
+ # Expressions
+
+ rule expression
+ space? (additive) <Expression>
+ end
+
+ rule comparative
+ primary space? comparative_operator space? primary <ComparativeExpression>
+ end
+
+ rule additive
+ multitive space? additive_operator space? additive <AdditiveExpression>
+ /
+ multitive
+ end
+
+ rule multitive
+ primary space? multitive_operator space? multitive <MultitiveExpression>
+ /
+ primary
+ end
+
+ rule primary
+ identifier / float / integer / string
+ /
+ '(' space? expression space? ')' <Expression>
+ end
+
+ ##################
+ # Operator sets
+
+ rule comparative_operator
+ equality_operator / inequality_operator / greater_than_operator / less_than_operator
+ end
+
+ rule additive_operator
+ addition_operator / subtraction_operator
+ end
+
+ rule multitive_operator
+ multiplication_operator / division_operator
+ end
+
+ ##################
+ # Operators
+
+ rule assignment_operator
+ '=' <AssignmentOperator>
+ end
+
+ rule addition_operator
+ '+' <AdditionOperator>
+ end
+
+ rule subtraction_operator
+ '-' <SubtractionOperator>
+ end
+
+ rule multiplication_operator
+ '*' <MultiplicationOperator>
+ end
+
+ rule division_operator
+ '/' <DivisionOperator>
+ end
+
+ rule equality_operator
+ '==' <EqualityOperator>
+ end
+
+ rule inequality_operator
+ '!=' <InequalityOperator>
+ end
+
+ rule greater_than_operator
+ '>' <GreaterThanOperator>
+ end
+
+ rule less_than_operator
+ '<' <LessThanOperator>
+ end
+
+ ##################
+ # Comments
+
+ rule comment
+ '#' [^"\n"]* ( "\n" / end_of_file )
+ end
+
+ ##################
+ # Whitespace
+
+ rule space
+ [\s]+
+ end
+
+ rule linebreak
+ [\n]+
+ end
+
+ ##################
+ # End of file
+
+ rule end_of_file
+ !.
+ end
+
+end
Oops, something went wrong.

0 comments on commit 664e9f9

Please sign in to comment.