Class: Oozby

Inherits:
Object
  • Object
show all
Defined in:
lib/oozby/base.rb,
lib/oozby/version.rb

Overview

Oozby class loads up files and evaluates them to a syntax tree, and renders to openscad source code

Defined Under Namespace

Modules: Geometry Classes: Element, Environment, Preprocessor, Render

Constant Summary collapse

Version =
'0.4.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOozby

Returns a new instance of Oozby.



7
8
9
10
11
# File 'lib/oozby/base.rb', line 7

def initialize
  @code_tree = []
  @filter_errors = false
  @debug = false
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/oozby/base.rb', line 5

def debug
  @debug
end

#filter_errorsObject

Returns the value of attribute filter_errors.



5
6
7
# File 'lib/oozby/base.rb', line 5

def filter_errors
  @filter_errors
end

Class Method Details

.versionObject



4
5
6
# File 'lib/oozby/version.rb', line 4

def self.version
  Oozby::Version
end

Instance Method Details

#abstract_treeObject



58
59
60
# File 'lib/oozby/base.rb', line 58

def abstract_tree
  @code_tree
end

#parse(code, filename: 'eval') ⇒ Object

parse oozby code in to a syntax tree



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oozby/base.rb', line 14

def parse code, filename: 'eval'
  env = Oozby::Environment.new(ooz: self)
  if File.exists? filename
    current_dir = Dir.pwd
    Dir.chdir File.dirname(filename)
  end
  
  # rescue block to filter out junk oozby library stuff from backtraces
  begin
    compiled = eval("lambda {; #{code}\n }", nil, filename)
    env._execute_oozby(&compiled)
  rescue StandardError, NoMethodError => err
    backtrace = $!.backtrace
    #backtrace = backtrace.select { |item| !item.include? __dir__ } unless backtrace.first.include? __dir__
    
    if @filter_errors # and backtrace.index { |i| i.include? ".oozby:" } < 3
      execute_oozby_idx = backtrace.index { |i| i.include? "in `_execute_oozby'" }
      backtrace = backtrace[0...execute_oozby_idx] if execute_oozby_idx
      backtrace.delete_if do |item|
        filename = item.split(':').first
        filename = File.realpath(filename)
        filename.start_with? __dir__
      end
    end
    
    raise $!, $!.message, backtrace
  end
  @code_tree = env._abstract_tree
  
ensure
  Dir.chdir current_dir if current_dir
end

#parse_file(filename) ⇒ Object

parse a file containing oozby code in to a syntax tree



48
49
50
# File 'lib/oozby/base.rb', line 48

def parse_file filename
  parse File.read(filename), filename: filename
end

#renderObject

render the last parsed oozby code in to openscad source code



53
54
55
56
# File 'lib/oozby/base.rb', line 53

def render
  renderer = Oozby::Render.new(ooz: self)
  renderer.render(@code_tree, clean: true).join("\n") + "\n"
end