File: source.go

package info (click to toggle)
mumax3 3.11.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 10,668 kB
  • sloc: makefile: 194; ansic: 155; sh: 86; javascript: 16
file content (28 lines) | stat: -rw-r--r-- 600 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package script

import (
	"go/ast"
	"go/token"
	"os"
)

func (w *World) compileSource(n *ast.CallExpr) Expr {
	if len(n.Args) != 1 {
		panic(err(n.Pos(), "source() needs 1 string argument, got", len(n.Args)))
	}
	arg := n.Args[0]
	if lit, ok := arg.(*ast.BasicLit); ok && lit.Kind == token.STRING {

		code, err1 := os.ReadFile(lit.Value[1 : len(lit.Value)-1])
		if err1 != nil {
			panic(err(n.Pos(), err1))
		}
		block, err2 := w.Compile(string(code))
		if err2 != nil {
			panic(err(n.Pos(), err2))
		}
		return block
	} else {
		panic(err(n.Pos(), "source() needs literal string argument"))
	}
}