Module: Liquidscript::Compiler::ICR::Expressions
- Included in:
- Liquidscript::Compiler::ICR
- Defined in:
- lib/liquidscript/compiler/icr/expressions.rb
Instance Method Summary collapse
-
#compile_assignment(identifier) ⇒ ICR::Code
Handles an assignment of the form ‘identifier = expression`, with the argument being the identifier, and the position of the compiler being after it.
-
#compile_expression ⇒ ICR::Code
Compiles an expression.
-
#compile_lparen ⇒ ICR::Code
We don’t know, at this point, whether this is a function declaration, or an expression; if it’s a function declaration, the contents of the lparen can only be commas and identifiers; if it’s an expression, it can have anything but commas in it.
Instance Method Details
#compile_assignment(identifier) ⇒ ICR::Code
Handles an assignment of the form ‘identifier = expression`, with the argument being the identifier, and the position of the compiler being after it.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 24 def compile_assignment(identifier) shift :equal value = compile_expression if identifier.type == :identifier variable = set(identifier) variable.value = value else variable = identifier end code :set, variable, value end |
#compile_expression ⇒ ICR::Code
Compiles an expression. This is primarily used in a general context, such that anything can be returned.
10 11 12 13 14 15 16 17 |
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 10 def compile_expression expect :number, :identifier, :dstring, :lparen, :sstring, :keyword, :lbrack => :object, :lbrace => :array, :arrow => :function end |
#compile_lparen ⇒ ICR::Code
We don’t know, at this point, whether this is a function declaration, or an expression; if it’s a function declaration, the contents of the lparen can only be commas and identifiers; if it’s an expression, it can have anything but commas in it.
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/liquidscript/compiler/icr/expressions.rb', line 44 def compile_lparen shift :lparen maybe_func = 1 components = [] unless peek?(:identifier, :rparen) maybe_func = 0 end expression = action do maybe_func = 0 components << compile_expression end loop do case maybe_func when 0 expect :rparen => action.end_loop, :_ => expression when 1 expect :rparen => action.end_loop, :comma => action { maybe_func = 2 }, :identifier => action { |i| components << i }, :_ => expression when 2 expect :rparen => action.end_loop, :comma => action.shift, :identifier => action { |i| components << i } end end func_decl = (maybe_func == 1 && peek?(:arrow)) || (maybe_func == 2) if func_decl compile_function_with_parameters(components) else code(:expression, components.map do |c| if c.is_a?(Scanner::Token) compile_identifier(c) else c end end) end end |