Class: Astrapi::Compiler

Inherits:
Object
  • Object
show all
Includes:
Dbg
Defined in:
lib/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dbg

#dbg

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



25
26
27
# File 'lib/compiler.rb', line 25

def initialize
  puts "ASTRAPI meta-compiler for Sexp-based DSLs [version #{VERSION}] (c) J-C Le Lann 2016"
end

Instance Attribute Details

#mmObject

Returns the value of attribute mm.



23
24
25
# File 'lib/compiler.rb', line 23

def mm
  @mm
end

Instance Method Details

#analyze_options(args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/compiler.rb', line 29

def analyze_options args
  args << "-h" if args.empty?
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: astrapi <file.mm> [options]"

    opts.on("-p", "--parse", "[P]arsing  only. AST generated internally") do |n|
      $options[:parse_only]=true
      $options[:verbosity]=2
    end

    opts.on("-c", "--check", "[C]hecking only. Semantic/contextual analysis ") do |n|
      $options[:check_only]=true
    end

    opts.on("--pp", "Pretty-print back the Astrapi code") do |n|
      $options[:pp_only]=true
    end

    opts.on("--version", "Prints version") do |n|
      puts VERSION
      abort
    end

    targets=[:ruby,:java,:python,:cpp]
    opts.on("--lang TARGET", "Target LANGUAGE (ruby by default,java,python,cpp)",targets) do |lang|
      case lang
      when :ruby
        $options[:target_language]=lang
      when :java,:python,:cpp
        raise "#{lang} version not implemented yet. Sorry"
        $options[:target_language]=lang
      else
        raise "illegal target language #{lang}"
      end
    end

    opts.on("-v LEVEL", "--verbose level", "allow verbose levels (0,1)") do |level|
      $options[:verbosity]=level.to_i
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  begin
    opt_parser.parse!(args)
  rescue Exception => e
    puts e
    dbg 0,e.backtrace if $options[:verbosity]>2
    exit
  end
  # remaining ARGV (remind that parse! has removed everything else)
  filename = ARGV.pop
  unless filename
    dbg 0,"Need to specify a filename to process"
    exit
  end
  return filename
end

#checkObject



115
116
117
118
# File 'lib/compiler.rb', line 115

def check
  puts "==> checking metamodel"
  Checker.new.check(mm)
end

#compile(mm_filename) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/compiler.rb', line 91

def compile mm_filename
  @mm=parse(mm_filename)
  pretty_print
  dot_print
  check
  generate_class_diagram
  generate_ruby
end

#dot_printObject



127
128
129
130
131
132
# File 'lib/compiler.rb', line 127

def dot_print
  target="#{mm.name.to_s.downcase}_ast.dot"
  puts "==> generate dot for metamodel........... #{target}"
  code=DotPrinter.new.print(mm)
  code.save_as(target,verbose=false)
end

#generate_class_diagramObject



120
121
122
123
124
125
# File 'lib/compiler.rb', line 120

def generate_class_diagram
  target="#{mm.name.to_s.downcase}_class_diagram.dot"
  puts "==> generating class diagram............. #{target}"
  code=ClassDiagramPrinter.new.print(mm)
  code.save_as(target,verbose=false)
end

#generate_rubyObject



134
135
136
137
# File 'lib/compiler.rb', line 134

def generate_ruby
  puts "==> generate software stack for DSL '#{@mm.name}'. Ruby version"
  RubyGenerator.new.generate(mm)
end

#parse(mm_filename) ⇒ Object



100
101
102
103
104
105
# File 'lib/compiler.rb', line 100

def parse mm_filename
  puts "==> parsing metamodel.................... #{mm_filename}"
  ast=Astrapi::Parser.new.parse(mm_filename)
  exit if $options[:parse_only]
  ast
end

#pretty_printObject



107
108
109
110
111
112
113
# File 'lib/compiler.rb', line 107

def pretty_print
  target="#{mm.name.to_s.downcase}_pp.mm"
  puts "==> pretty print metamodel............... #{target}"
  code=PrettyPrinter.new.print(mm)
  code.save_as(target,verbose=false)
  exit if $options[:pp_only]
end