Sha256: 237be2e003e629503d97f08cecfc101ef03a92253eeb5253bbd43e2ea8ebfef6

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

grammar SimpleSem
  include Arithmetic
  
  rule statement
    set_stmt / jump_stmt / jumpt_stmt / halt
  end
  
  rule halt
    'halt' {
      def execute(env={})
        raise ProgramHalt
      end
    }
  end
  
  rule set_stmt
    set_stmt_assign / set_stmt_write / set_stmt_read
  end

  rule set_stmt_assign
    'set' space loc:additive comma value:additive {
      def execute(env)
        env.data[loc.eval(env)] = value.eval(env)
      end
    }
  end
  
  rule set_stmt_write
    'set' space 'write' comma expression {
      def execute(env)
        puts expression.eval(env)
      end
    }
    /
    'set' space 'write' comma '"' string:(!'"' . )* '"' {
      def execute(env)
        puts string.text_value
      end
    }
  end
  
  rule set_stmt_read
    'set' space loc:additive comma 'read' {
      def execute(env)
        print "input: "
        env.data[loc.eval(env)] = $stdin.gets.strip.to_i
      end  
    }
  end
  
  rule jump_stmt
    'jump' space loc:additive {
      def execute(env)
        env.pc = loc.eval(env)
      end
    }
  end
  
  rule jumpt_stmt
    'jumpt' space loc:additive comma expression {
      def execute(env)
        if expression.eval(env)
          env.pc = loc.eval(env)
        end
      end
    }
  end
  
  rule primary
    ip
    /
    data_lookup
    /
    number
    /
    '(' space expression space ')' {
      def eval(env={})
        expression.eval(env)
      end
    }
  end
  
  rule data_lookup
    'D[' expr:additive ']' {
      def eval(env)
        env.data[expr.eval(env)]
      end
    }
  end
  
  rule ip
    'ip' {
      def eval(env)
        env.pc
      end
    }
  end
  
  rule comma
    space ',' space
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
robolson-simplesem-0.1.0 lib/simplesem/simple_sem.treetop
robolson-simplesem-0.1.1 lib/simplesem/simple_sem.treetop
robolson-simplesem-0.1.2 lib/simplesem/simple_sem.treetop