Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

What is proper way to run multi-line expr scripts? I seem to get errors when trying... #717

Answered by antonmedv
nwcs-sh asked this question in General
Discussion options

Hello!

I am playing with expr and my use case is multi-line re-use expr scripts, am trying to do that like so:

func main() {
	env := map[string]interface{}{
		"println": fmt.Println,
	}
	code := `
let foo = "asdf";
let bar = "fsda";
println(foo);
println(bar)
`
	program, err := expr.Compile(code, expr.Env(env))
	if err != nil {
		panic(err)
	}
	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}
	fmt.Println(output)
}

I've tried variations of println(foo), no ;, etc. But it seems like I can only execute one func at a time?

I get either the following errors:

panic: unexpected token Operator(";") (4:13)
	 | println(foo);
	 | ............^
panic: unexpected token Identifier("println") (5:1)
	 | println(bar)
	 | ^
You must be logged in to vote

I've added support for SequenceNode! Now Expr, starting with 1.17, will support multiline expressions!

foo();
bar();
let x = baz();
x + 1

Replies: 2 comments

Comment options

Currenly there is no way to do those. As Expr is designed to work with expressions.

One "hack" to overcome this, is to return an expression:

let foo = "asdf";
let bar = "fsda";
[
 println(foo),
 println(bar),
]

Also I think we should add support for statments in Expr as well via a feature flag.

You must be logged in to vote
0 replies
Comment options

I've added support for SequenceNode! Now Expr, starting with 1.17, will support multiline expressions!

foo();
bar();
let x = baz();
x + 1
You must be logged in to vote
0 replies
Answer selected by antonmedv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /