Example #1
0
func runFile(name string) *parser.Block {
	os.Chdir(filepath.Dir(name))

	file, err := util.NewFile(filepath.Base(name) + "." + util.FileExtension)
	if err != nil {
		reportError(err.Error(), false)
	}

	lex := lexer.NewLexerFromFile(file)

	err = lex.Lex()
	if err != nil {
		reportError(err.Error(), false)
	}

	nodes, err := parser.Nodes(lex.Tokens)
	if err != nil {
		reportError(err.Error(), false)
	}

	block := parser.NewBlock(nodes, nil, file)
	setDefaults(block.Scope)

	err = block.Exec()
	if err != nil {
		reportError(err.Error(), false)
	}

	return block
}
Example #2
0
func useFile(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "use-file", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	file, err := util.NewFile(values[0].S)
	if err != nil {
		return err
	}

	lex := lexer.NewLexerFromFile(file)

	err = lex.Lex()
	if err != nil {
		return err
	}

	nodes, err := parser.Nodes(lex.Tokens)
	if err != nil {
		return err
	}

	fileBlock := parser.NewBlock(nodes, context.Block, file)
	err = fileBlock.Exec()
	if err != nil {
		return err
	}

	for key, value := range fileBlock.Scope.Symbols {
		if value.Exported {
			context.Block.Scope.SetSymbol(key, value)
		}
	}

	fileBlock.Stack.PopAllToOtherStack(context.Block.Stack)

	return nil
}
Example #3
0
func runRepl() *parser.Block {
	var tokens []*lexer.Token
	depth := 0

	block := parser.NewBlock(nil, nil, nil)
	setDefaults(block.Scope)

	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("> ")
		data, _ := reader.ReadString('\n')

		lex := lexer.NewLexer([]rune(data))

		err := lex.Lex()
		if err != nil {
			reportError(err.Error(), true)
		} else {
			for _, token := range lex.Tokens {
				tokens = append(tokens, token)
			}

			for _, token := range lex.Tokens {
				if token.IsOpening() {
					depth++
				} else if token.IsClosing() {
					depth--
				}
			}

			if depth <= 0 {
				nodes, err := parser.Nodes(tokens)

				tokens = nil
				depth = 0

				if err != nil {
					reportError(err.Error(), true)
				} else {
					block.Nodes = nodes

					err := block.Exec()
					if err != nil {
						reportError(err.Error(), true)
					}

					if block.Stack.Len() > 0 {
						fmt.Print(util.COLOR_YELLOW)

						for i := 0; i < block.Stack.Len(); i++ {
							fmt.Print(block.Stack.Peek(i).(*parser.Value).String() + " ")
						}

						fmt.Print(util.COLOR_RESET + "\n")
					}
				}
			}
		}
	}

	return block
}