Module: SpecialGiggle

Includes:
Generator, Lexer, Parser
Defined in:
lib/special-giggle.rb,
lib/special-giggle/version.rb

Defined Under Namespace

Classes: GiggleError

Constant Summary collapse

VERSION =
"1.0.0"

Constants included from Generator

Generator::BinaryOps, Generator::UnaryOps, Generator::Vars

Constants included from Parser

Parser::BinaryOps, Parser::UnaryOps

Constants included from Lexer

Lexer::Patterns

Class Method Summary collapse

Methods included from Generator

generate, generate_expression, generate_statement, var_temp

Methods included from Parser

parse, parse_expression, parse_statement

Methods included from Lexer

next_token, tokenize

Class Method Details

.compile(infile, outfile, tmpdir = "/tmp") ⇒ Object

Raises:



19
20
21
22
23
24
25
26
# File 'lib/special-giggle.rb', line 19

def self.compile(infile, outfile, tmpdir = "/tmp")
  raise GiggleError, "Input file #{infile} does not found." unless File.exist?(infile)
  file = File.basename(infile, ".*")
  sfile, ofile = File.expand_path("#{file}.s", tmpdir), File.expand_path("#{file}.o", tmpdir)
  File.write(sfile, to_asm(File.read(infile)))
  system("as --64 -o #{ofile} #{sfile}") || (raise GiggleError, "Can't compile with as")
  system("gcc -no-pie -o #{outfile} #{ofile}") || (raise GiggleError, "Can't link with gcc")
end

.to_asm(string) ⇒ Object



12
13
14
15
16
17
# File 'lib/special-giggle.rb', line 12

def self.to_asm(string)
  tokens = Lexer.tokenize(string)
  ast = Parser.parse(tokens)
  code = Generator.generate(ast)
  code
end