Beispiel #1
0
// Evaluator
func (vm *vm) EvaluateStringInContext(input string, context Value) (Value, error) {
	parser.Reset()

	lexer := parser.NewLexer(input)
	result := parser.RubyParse(lexer)
	if result != 0 {
		return nil, NewParseError(vm.currentFilename)
	}

	return vm.executeWithContext(context, parser.Statements...)
}
Beispiel #2
0
func (vm *vm) EvaluateStringInContextAndNewStack(input string, context Value) (Value, error) {
	parser.Reset()

	lexer := parser.NewLexer(input)
	result := parser.RubyParse(lexer)
	if result != 0 {
		return nil, NewParseError(vm.currentFilename)
	}

	vm.localVariableStack.Unshift()
	defer vm.localVariableStack.Shift()
	return vm.executeWithContext(context, parser.Statements...)
}
Beispiel #3
0
func (vm *vm) Run(input string) (Value, error) {
	parser.Reset()

	lexer := parser.NewLexer(input)
	result := parser.RubyParse(lexer)
	if result != 0 {
		return nil, NewParseError(vm.currentFilename)
	}

	main := vm.ObjectSpace["main"]
	vm.stack.Unshift("main", vm.currentFilename, 0)
	defer vm.stack.Shift()

	vm.localVariableStack.Unshift()
	defer vm.localVariableStack.Shift()
	return vm.executeWithContext(main, parser.Statements...)
}
Beispiel #4
0
func main() {
	stdin := bufio.NewReader(os.NewFile(0, "stdin"))

	for {
		fmt.Printf("Give me your best line of ruby: ")

		if line, err := stdin.ReadString('\n'); err == nil {
			lexer := parser.NewLexer(line)

			if parser.RubyParse(lexer) != 0 {
				break
			}

			statements := parser.Statements
			fmt.Printf("you typed '%#v'\n", statements[len(statements)-1])
		} else {
			println("FAIL:", err.Error())
			break
		}
	}
}