示例#1
0
文件: main.go 项目: raoulvdberge/risp
func main() {
	flag.Usage = usage
	flag.Parse()

	if len(flag.Args()) > 0 {
		var file *util.File

		util.Timed("file reading", *debug, func() {
			os.Chdir(filepath.Dir(flag.Arg(0)))

			f, err := util.NewFile(filepath.Base(flag.Arg(0)))

			if err != nil {
				util.ReportError(err, false)
			}

			file = f
		})

		run(lexer.NewSourceFromFile(file))
	} else if *runRepl {
		s := repl.NewReplSession(apply(runtime.NewBlock(nil, runtime.NewScope(nil))))
		s.Run()
	} else {
		bytes, err := ioutil.ReadAll(os.Stdin)

		if err != nil {
			util.ReportError(err, false)
		}

		run(lexer.NewSourceFromString("<stdin>", string(bytes)))
	}
}
示例#2
0
func builtinLoad(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue); err != nil {
		return nil, err
	}

	file, err := util.NewFile(context.Args[0].Str)

	if err != nil {
		return nil, err
	}

	l := lexer.NewLexer(lexer.NewSourceFromFile(file))
	util.ReportError(l.Lex(), false)

	p := parser.NewParser(l.Tokens)
	util.ReportError(p.Parse(), false)

	b := runtime.NewBlock(p.Nodes, runtime.NewScope(context.Block.Scope))

	result, err := b.Eval()

	if err != nil {
		return nil, err
	}

	for key, value := range b.Scope.Symbols {
		if value.Exported {
			if value.Value.Type == runtime.FunctionValue {
				value.Value.Function.CustomScope = b.Scope
			}

			context.Block.Scope.SetSymbol(b.SymbolName(key), value)
		}
	}

	return result, nil
}