2014-03-03 13:41:44 +08:00
|
|
|
package eval
|
|
|
|
|
2014-04-02 10:51:09 +08:00
|
|
|
// Builtin special forms.
|
2014-03-03 13:41:44 +08:00
|
|
|
|
2015-01-25 06:45:34 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-01-25 07:31:52 +08:00
|
|
|
"os"
|
2015-02-10 21:50:58 +08:00
|
|
|
"path"
|
2015-02-24 01:36:46 +08:00
|
|
|
"strings"
|
2015-01-25 06:45:34 +08:00
|
|
|
|
|
|
|
"github.com/elves/elvish/parse"
|
|
|
|
)
|
2014-03-03 13:41:44 +08:00
|
|
|
|
2015-02-25 19:21:06 +08:00
|
|
|
type exitusOp func(*evalCtx) exitus
|
2015-02-25 22:04:10 +08:00
|
|
|
type builtinSpecialCompile func(*compileCtx, *parse.Form) exitusOp
|
2014-04-02 10:51:09 +08:00
|
|
|
|
|
|
|
type builtinSpecial struct {
|
2015-01-27 02:11:05 +08:00
|
|
|
compile builtinSpecialCompile
|
2014-04-02 10:51:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var builtinSpecials map[string]builtinSpecial
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Needed to avoid initialization loop
|
|
|
|
builtinSpecials = map[string]builtinSpecial{
|
2015-01-27 02:11:05 +08:00
|
|
|
"var": builtinSpecial{compileVar},
|
|
|
|
"set": builtinSpecial{compileSet},
|
|
|
|
"del": builtinSpecial{compileDel},
|
2015-01-22 03:36:41 +08:00
|
|
|
|
2015-02-10 21:50:58 +08:00
|
|
|
"use": builtinSpecial{compileUse},
|
|
|
|
|
2015-01-27 02:11:05 +08:00
|
|
|
"fn": builtinSpecial{compileFn},
|
2015-02-10 18:43:01 +08:00
|
|
|
"if": builtinSpecial{compileIf},
|
2015-01-22 03:36:41 +08:00
|
|
|
|
2015-01-27 02:11:05 +08:00
|
|
|
"static-typeof": builtinSpecial{compileStaticTypeof},
|
2014-04-02 10:51:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 02:22:03 +08:00
|
|
|
func mayAssign(tvar, tval Type) bool {
|
|
|
|
if isAny(tval) || isAny(tvar) {
|
|
|
|
return true
|
|
|
|
}
|
2015-01-23 07:38:44 +08:00
|
|
|
// XXX(xiaq) This is not how you check the equality of two interfaces. But
|
|
|
|
// it happens to work when all the Type instances we have are empty
|
|
|
|
// structs.
|
2015-01-23 02:22:03 +08:00
|
|
|
return tval == tvar
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
func checkSetType(cc *compileCtx, names []string, values []*parse.Compound, vop valuesOp, p parse.Pos) {
|
2015-02-26 21:50:48 +08:00
|
|
|
n, more := vop.tr.count()
|
2014-09-17 23:45:32 +08:00
|
|
|
if more {
|
2015-02-26 21:50:48 +08:00
|
|
|
if n > len(names) {
|
|
|
|
cc.errorf(p, "number of variables (%d) can never match that of values (%d or more)", len(names), n)
|
|
|
|
}
|
|
|
|
// Only check the variables before the "more" part.
|
2015-02-26 21:57:01 +08:00
|
|
|
names = names[:n]
|
2015-02-26 21:50:48 +08:00
|
|
|
} else if n != len(names) {
|
|
|
|
cc.errorf(p, "number of variables (%d) doesn't match that of values (%d or more)", len(names), n)
|
2014-09-17 23:45:32 +08:00
|
|
|
}
|
2015-02-26 21:50:48 +08:00
|
|
|
|
2014-09-26 05:26:56 +08:00
|
|
|
for i, name := range names {
|
2015-01-20 05:00:14 +08:00
|
|
|
tval := vop.tr[i].t
|
2015-02-25 22:04:10 +08:00
|
|
|
tvar := cc.ResolveVar(splitQualifiedName(name))
|
2015-01-23 02:22:03 +08:00
|
|
|
if !mayAssign(tvar, tval) {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(values[i].Pos, "type mismatch: assigning %#v value to %#v variable", tval, tvar)
|
2014-07-19 16:53:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 23:46:40 +08:00
|
|
|
// ensure that a CompoundNode contains exactly one PrimaryNode.
|
2015-02-25 22:04:10 +08:00
|
|
|
func ensurePrimary(cc *compileCtx, cn *parse.Compound, msg string) *parse.Primary {
|
2014-09-26 05:26:56 +08:00
|
|
|
if len(cn.Nodes) != 1 || cn.Nodes[0].Right != nil {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(cn.Pos, msg)
|
2014-09-26 05:26:56 +08:00
|
|
|
}
|
|
|
|
return cn.Nodes[0].Left
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:22:55 +08:00
|
|
|
// ensureVariableOrStringPrimary ensures that a CompoundNode contains exactly
|
|
|
|
// one PrimaryNode of type VariablePrimary or StringPrimary.
|
2015-02-25 22:04:10 +08:00
|
|
|
func ensureVariableOrStringPrimary(cc *compileCtx, cn *parse.Compound, msg string) (*parse.Primary, string) {
|
|
|
|
pn := ensurePrimary(cc, cn, msg)
|
2015-01-19 23:46:40 +08:00
|
|
|
switch pn.Typ {
|
|
|
|
case parse.VariablePrimary, parse.StringPrimary:
|
2015-02-25 04:59:14 +08:00
|
|
|
return pn, pn.Node.(*parse.String).Text
|
2015-01-19 23:46:40 +08:00
|
|
|
default:
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(cn.Pos, msg)
|
2015-01-19 23:46:40 +08:00
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:22:55 +08:00
|
|
|
// ensureVariablePrimary ensures that a CompoundNode contains exactly one
|
|
|
|
// PrimaryNode of type VariablePrimary.
|
2015-02-25 22:04:10 +08:00
|
|
|
func ensureVariablePrimary(cc *compileCtx, cn *parse.Compound, msg string) (*parse.Primary, string) {
|
|
|
|
pn, text := ensureVariableOrStringPrimary(cc, cn, msg)
|
2015-01-22 00:22:55 +08:00
|
|
|
if pn.Typ != parse.VariablePrimary {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(pn.Pos, msg)
|
2015-01-22 00:22:55 +08:00
|
|
|
}
|
|
|
|
return pn, text
|
|
|
|
}
|
|
|
|
|
2015-02-10 16:32:58 +08:00
|
|
|
// ensureStringPrimary ensures that a CompoundNode contains exactly one
|
|
|
|
// PrimaryNode of type VariablePrimary.
|
2015-02-25 22:04:10 +08:00
|
|
|
func ensureStringPrimary(cc *compileCtx, cn *parse.Compound, msg string) (*parse.Primary, string) {
|
|
|
|
pn, text := ensureVariableOrStringPrimary(cc, cn, msg)
|
2015-02-10 16:32:58 +08:00
|
|
|
if pn.Typ != parse.StringPrimary {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(pn.Pos, msg)
|
2015-02-10 16:32:58 +08:00
|
|
|
}
|
|
|
|
return pn, text
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:22:55 +08:00
|
|
|
// ensureStartWithVariabl ensures the first compound of the form is a
|
|
|
|
// VariablePrimary. This is merely for better error messages; No actual
|
|
|
|
// processing is done.
|
2015-02-25 22:04:10 +08:00
|
|
|
func ensureStartWithVariable(cc *compileCtx, fn *parse.Form, form string) {
|
2015-01-19 23:46:40 +08:00
|
|
|
if len(fn.Args.Nodes) == 0 {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fn.Pos, "expect variable after %s", form)
|
2015-01-19 23:46:40 +08:00
|
|
|
}
|
2015-02-25 22:04:10 +08:00
|
|
|
ensureVariablePrimary(cc, fn.Args.Nodes[0], "expect variable")
|
2015-01-19 23:46:40 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 00:22:55 +08:00
|
|
|
// VarForm = 'var' { VarGroup } [ '=' Compound ]
|
|
|
|
// VarGroup = { VariablePrimary } [ StringPrimary ]
|
2015-01-19 23:46:40 +08:00
|
|
|
//
|
|
|
|
// Variables in the same VarGroup has the type specified by the StringPrimary.
|
2015-01-22 00:22:55 +08:00
|
|
|
// Only in the last VarGroup the StringPrimary may be omitted, in which case it
|
|
|
|
// defaults to "any". For instance,
|
2015-01-19 23:46:40 +08:00
|
|
|
//
|
|
|
|
// var $u $v Type1 $x $y Type2 $z = a b c d e
|
|
|
|
//
|
|
|
|
// gives $u and $v type Type1, $x $y type Type2 and $z type Any and
|
|
|
|
// assigns them the values a, b, c, d, e respectively.
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileVar(cc *compileCtx, fn *parse.Form) exitusOp {
|
2014-09-26 05:26:56 +08:00
|
|
|
var (
|
|
|
|
names []string
|
|
|
|
types []Type
|
2015-02-25 04:59:14 +08:00
|
|
|
values []*parse.Compound
|
2014-09-26 05:26:56 +08:00
|
|
|
)
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
ensureStartWithVariable(cc, fn, "var")
|
2014-09-26 05:26:56 +08:00
|
|
|
|
2015-01-19 23:46:40 +08:00
|
|
|
for i, cn := range fn.Args.Nodes {
|
|
|
|
expect := "expect variable, type or equal sign"
|
2015-02-25 22:04:10 +08:00
|
|
|
pn, text := ensureVariableOrStringPrimary(cc, cn, expect)
|
2015-01-19 23:46:40 +08:00
|
|
|
if pn.Typ == parse.VariablePrimary {
|
|
|
|
names = append(names, text)
|
2014-03-03 13:41:44 +08:00
|
|
|
} else {
|
2015-01-19 23:46:40 +08:00
|
|
|
if text == "=" {
|
|
|
|
values = fn.Args.Nodes[i+1:]
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if t, ok := typenames[text]; !ok {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(pn.Pos, "%v is not a valid type name", text)
|
2014-09-26 05:26:56 +08:00
|
|
|
} else {
|
2015-01-19 23:46:40 +08:00
|
|
|
if len(names) == len(types) {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(pn.Pos, "duplicate type")
|
2014-09-26 05:26:56 +08:00
|
|
|
}
|
|
|
|
for i := len(types); i < len(names); i++ {
|
|
|
|
types = append(types, t)
|
|
|
|
}
|
2014-03-30 16:02:56 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-03 13:41:44 +08:00
|
|
|
}
|
2015-01-19 23:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(types); i < len(names); i++ {
|
2015-01-27 02:05:07 +08:00
|
|
|
types = append(types, anyType{})
|
2014-09-26 05:26:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, name := range names {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.pushVar(name, types[i])
|
2014-03-03 13:41:44 +08:00
|
|
|
}
|
2014-09-26 05:26:56 +08:00
|
|
|
|
|
|
|
var vop valuesOp
|
|
|
|
if values != nil {
|
2015-02-25 22:04:10 +08:00
|
|
|
vop = cc.compounds(values)
|
|
|
|
checkSetType(cc, names, values, vop, fn.Pos)
|
2014-09-26 05:26:56 +08:00
|
|
|
}
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
2014-09-26 05:26:56 +08:00
|
|
|
for i, name := range names {
|
2015-02-25 19:21:06 +08:00
|
|
|
ec.local[name] = newInternalVariable(types[i].Default(), types[i])
|
2014-04-30 10:45:54 +08:00
|
|
|
}
|
2014-09-26 05:26:56 +08:00
|
|
|
if vop.f != nil {
|
2015-02-25 19:21:06 +08:00
|
|
|
return doSet(ec, names, vop.f(ec))
|
2014-04-30 10:45:54 +08:00
|
|
|
}
|
2015-01-22 06:43:30 +08:00
|
|
|
return success
|
2014-09-26 05:26:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 23:46:40 +08:00
|
|
|
// SetForm = 'set' { VariablePrimary } '=' { Compound }
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileSet(cc *compileCtx, fn *parse.Form) exitusOp {
|
2014-09-26 05:26:56 +08:00
|
|
|
var (
|
|
|
|
names []string
|
2015-02-25 04:59:14 +08:00
|
|
|
values []*parse.Compound
|
2014-09-26 05:26:56 +08:00
|
|
|
)
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
ensureStartWithVariable(cc, fn, "set")
|
2014-09-26 05:26:56 +08:00
|
|
|
|
2015-01-19 23:46:40 +08:00
|
|
|
for i, cn := range fn.Args.Nodes {
|
|
|
|
expect := "expect variable or equal sign"
|
2015-02-25 22:04:10 +08:00
|
|
|
pn, text := ensureVariableOrStringPrimary(cc, cn, expect)
|
2015-01-19 23:46:40 +08:00
|
|
|
if pn.Typ == parse.VariablePrimary {
|
2015-01-25 06:16:27 +08:00
|
|
|
ns, name := splitQualifiedName(text)
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.mustResolveVar(ns, name, cn.Pos)
|
2015-01-19 23:46:40 +08:00
|
|
|
names = append(names, text)
|
|
|
|
} else {
|
|
|
|
if text != "=" {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(pn.Pos, expect)
|
2014-04-30 10:45:54 +08:00
|
|
|
}
|
2015-01-19 23:46:40 +08:00
|
|
|
values = fn.Args.Nodes[i+1:]
|
|
|
|
break
|
2014-03-30 16:02:56 +08:00
|
|
|
}
|
2014-03-30 10:10:06 +08:00
|
|
|
}
|
2014-03-03 13:41:44 +08:00
|
|
|
|
2014-09-26 05:26:56 +08:00
|
|
|
var vop valuesOp
|
2015-02-25 22:04:10 +08:00
|
|
|
vop = cc.compounds(values)
|
|
|
|
checkSetType(cc, names, values, vop, fn.Pos)
|
2014-03-20 13:19:30 +08:00
|
|
|
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
|
|
|
return doSet(ec, names, vop.f(ec))
|
2014-09-26 05:26:56 +08:00
|
|
|
}
|
2014-03-20 13:19:30 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 06:43:30 +08:00
|
|
|
var (
|
2015-01-27 01:53:08 +08:00
|
|
|
arityMismatch = newFailure("arity mismatch")
|
|
|
|
typeMismatch = newFailure("type mismatch")
|
2015-01-22 06:43:30 +08:00
|
|
|
)
|
|
|
|
|
2015-02-25 19:21:06 +08:00
|
|
|
func doSet(ec *evalCtx, names []string, values []Value) exitus {
|
2014-03-20 13:50:13 +08:00
|
|
|
// TODO Support assignment of mismatched arity in some restricted way -
|
|
|
|
// "optional" and "rest" arguments and the like
|
|
|
|
if len(names) != len(values) {
|
2015-01-22 06:43:30 +08:00
|
|
|
return arityMismatch
|
2014-03-20 13:50:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, name := range names {
|
|
|
|
// TODO Prevent overriding builtin variables e.g. $pid $env
|
2015-02-25 19:21:06 +08:00
|
|
|
variable := ec.ResolveVar(splitQualifiedName(name))
|
2015-01-25 06:45:34 +08:00
|
|
|
if variable == nil {
|
|
|
|
return newFailure(fmt.Sprintf("variable $%s not found; the compiler has a bug", name))
|
|
|
|
}
|
2015-01-25 06:40:34 +08:00
|
|
|
tvar := variable.StaticType()
|
2015-01-23 02:22:03 +08:00
|
|
|
tval := values[i].Type()
|
|
|
|
if !mayAssign(tvar, tval) {
|
|
|
|
return typeMismatch
|
|
|
|
}
|
2015-01-25 06:40:34 +08:00
|
|
|
variable.Set(values[i])
|
2014-03-20 13:50:13 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 06:43:30 +08:00
|
|
|
return success
|
2014-03-20 13:50:13 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 00:22:55 +08:00
|
|
|
// DelForm = 'del' { VariablePrimary }
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileDel(cc *compileCtx, fn *parse.Form) exitusOp {
|
2014-09-16 23:57:33 +08:00
|
|
|
// Do conventional compiling of all compound expressions, including
|
|
|
|
// ensuring that variables can be resolved
|
2015-01-25 07:05:47 +08:00
|
|
|
var names, envNames []string
|
2015-01-22 00:22:55 +08:00
|
|
|
for _, cn := range fn.Args.Nodes {
|
2015-02-25 22:04:10 +08:00
|
|
|
_, qname := ensureVariablePrimary(cc, cn, "expect variable")
|
2015-01-24 07:42:57 +08:00
|
|
|
ns, name := splitQualifiedName(qname)
|
2015-01-25 07:05:47 +08:00
|
|
|
switch ns {
|
|
|
|
case "", "local":
|
2015-02-25 22:04:10 +08:00
|
|
|
if cc.resolveVarOnThisScope(name) == nil {
|
|
|
|
cc.errorf(cn.Pos, "variable $%s not found on current local scope", name)
|
2015-01-25 07:05:47 +08:00
|
|
|
}
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.popVar(name)
|
2015-01-25 07:05:47 +08:00
|
|
|
names = append(names, name)
|
|
|
|
case "env":
|
|
|
|
envNames = append(envNames, name)
|
|
|
|
default:
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(cn.Pos, "can only delete a variable in local: or env: namespace")
|
2014-04-02 11:13:43 +08:00
|
|
|
}
|
2015-01-22 00:22:55 +08:00
|
|
|
|
2014-04-02 11:13:43 +08:00
|
|
|
}
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
2014-09-26 05:26:56 +08:00
|
|
|
for _, name := range names {
|
2015-02-25 19:21:06 +08:00
|
|
|
delete(ec.local, name)
|
2014-04-30 10:45:54 +08:00
|
|
|
}
|
2015-01-25 07:05:47 +08:00
|
|
|
for _, name := range envNames {
|
2015-02-26 21:54:23 +08:00
|
|
|
// XXX(xiaq): We rely on the fact that os.Unsetenv always returns a
|
|
|
|
// nil error.
|
2015-01-25 07:31:52 +08:00
|
|
|
os.Unsetenv(name)
|
2015-01-25 07:05:47 +08:00
|
|
|
}
|
2015-01-22 06:43:30 +08:00
|
|
|
return success
|
2014-04-02 11:13:43 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 05:11:44 +08:00
|
|
|
|
2015-02-10 21:50:58 +08:00
|
|
|
func stem(fname string) string {
|
|
|
|
base := path.Base(fname)
|
|
|
|
ext := path.Ext(base)
|
|
|
|
return base[0 : len(base)-len(ext)]
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseForm = 'use' StringPrimary.modname Primary.fname
|
|
|
|
// = 'use' StringPrimary.fname
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileUse(cc *compileCtx, fn *parse.Form) exitusOp {
|
2015-02-25 04:59:14 +08:00
|
|
|
var fnameNode *parse.Compound
|
2015-02-10 21:50:58 +08:00
|
|
|
var fname, modname string
|
|
|
|
|
|
|
|
switch len(fn.Args.Nodes) {
|
|
|
|
case 0:
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fn.Args.Pos, "expect module name or file name")
|
2015-02-10 21:50:58 +08:00
|
|
|
case 1, 2:
|
|
|
|
fnameNode = fn.Args.Nodes[0]
|
2015-02-25 22:04:10 +08:00
|
|
|
_, fname = ensureStringPrimary(cc, fnameNode, "expect string literal")
|
2015-02-10 21:50:58 +08:00
|
|
|
if len(fn.Args.Nodes) == 2 {
|
|
|
|
modnameNode := fn.Args.Nodes[1]
|
|
|
|
_, modname = ensureStringPrimary(
|
2015-02-25 22:04:10 +08:00
|
|
|
cc, modnameNode, "expect string literal")
|
2015-02-10 21:50:58 +08:00
|
|
|
if modname == "" {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(modnameNode.Pos, "module name is empty")
|
2015-02-10 21:50:58 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
modname = stem(fname)
|
|
|
|
if modname == "" {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fnameNode.Pos, "stem of file name is empty")
|
2015-02-10 21:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fn.Args.Nodes[2].Pos, "superfluous argument")
|
2015-02-10 21:50:58 +08:00
|
|
|
}
|
2015-02-24 01:36:46 +08:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(fname, "/"):
|
|
|
|
// Absolute file name, do nothing
|
|
|
|
case strings.HasPrefix(fname, "./") || strings.HasPrefix(fname, "../"):
|
|
|
|
// File name relative to current source
|
2015-02-25 22:04:10 +08:00
|
|
|
fname = path.Clean(path.Join(cc.dir, fname))
|
2015-02-24 01:36:46 +08:00
|
|
|
default:
|
|
|
|
// File name relative to data dir
|
2015-02-25 22:04:10 +08:00
|
|
|
fname = path.Clean(path.Join(cc.dataDir, fname))
|
2015-02-24 01:36:46 +08:00
|
|
|
}
|
2015-02-10 21:50:58 +08:00
|
|
|
src, err := readFileUTF8(fname)
|
|
|
|
if err != nil {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fnameNode.Pos, "cannot read module: %s", err.Error())
|
2015-02-10 21:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cn, err := parse.Parse(fname, src)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(xiaq): Pretty print
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fnameNode.Pos, "cannot parse module: %s", err.Error())
|
2015-02-10 21:50:58 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
newCc := &compileCtx{
|
|
|
|
cc.Compiler,
|
|
|
|
fname, src, path.Dir(fname),
|
|
|
|
[]staticNS{staticNS{}}, staticNS{},
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := newCc.compile(cn)
|
2015-02-10 21:50:58 +08:00
|
|
|
if err != nil {
|
|
|
|
// TODO(xiaq): Pretty print
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fnameNode.Pos, "cannot compile module: %s", err.Error())
|
2015-02-10 21:50:58 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.mod[modname] = newCc.scopes[0]
|
2015-02-10 21:50:58 +08:00
|
|
|
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
|
|
|
// TODO(xiaq): Should install a failHandler that fails the use call
|
|
|
|
newEc := &evalCtx{
|
|
|
|
ec.Evaler,
|
|
|
|
fname, src, "module " + modname,
|
|
|
|
ns{}, ns{},
|
|
|
|
ec.ports, nil,
|
|
|
|
}
|
|
|
|
op(newEc)
|
|
|
|
ec.mod[modname] = newEc.local
|
2015-02-10 21:50:58 +08:00
|
|
|
return success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 01:06:14 +08:00
|
|
|
// FnForm = 'fn' StringPrimary { VariablePrimary } ClosurePrimary
|
|
|
|
//
|
|
|
|
// fn defines a function. This isn't strictly needed, since user-defined
|
|
|
|
// functions are just variables. The following two lines should be exactly
|
|
|
|
// equivalent:
|
|
|
|
//
|
|
|
|
// fn f $a $b { put (* $a $b) (/ $a *b) }
|
|
|
|
// var $fn-f = { |$a $b| put (* $a $b) (/ $a $b) }
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileFn(cc *compileCtx, fn *parse.Form) exitusOp {
|
2015-01-22 01:06:14 +08:00
|
|
|
if len(fn.Args.Nodes) == 0 {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fn.Pos, "expect function name after fn")
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
2015-02-25 22:04:10 +08:00
|
|
|
_, fnName := ensureStringPrimary(cc, fn.Args.Nodes[0], "expect string literal")
|
2015-02-24 17:21:27 +08:00
|
|
|
varName := fnPrefix + fnName
|
2015-01-22 01:06:14 +08:00
|
|
|
|
2015-02-25 04:59:14 +08:00
|
|
|
var closureNode *parse.Closure
|
|
|
|
var argNames []*parse.Compound
|
2015-01-22 01:06:14 +08:00
|
|
|
|
|
|
|
for i, cn := range fn.Args.Nodes[1:] {
|
|
|
|
expect := "expect variable or closure"
|
2015-02-25 22:04:10 +08:00
|
|
|
pn := ensurePrimary(cc, cn, expect)
|
2015-01-22 01:06:14 +08:00
|
|
|
switch pn.Typ {
|
|
|
|
case parse.ClosurePrimary:
|
|
|
|
if i+2 != len(fn.Args.Nodes) {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(fn.Args.Nodes[i+2].Pos, "garbage after closure literal")
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
2015-02-25 04:59:14 +08:00
|
|
|
closureNode = pn.Node.(*parse.Closure)
|
2015-01-22 01:06:14 +08:00
|
|
|
break
|
|
|
|
case parse.VariablePrimary:
|
|
|
|
argNames = append(argNames, cn)
|
|
|
|
default:
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(pn.Pos, expect)
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(argNames) > 0 {
|
2015-02-25 04:59:14 +08:00
|
|
|
closureNode = &parse.Closure{
|
2015-01-22 01:06:14 +08:00
|
|
|
closureNode.Pos,
|
2015-02-25 04:59:14 +08:00
|
|
|
&parse.Spaced{argNames[0].Pos, argNames},
|
2015-01-22 01:06:14 +08:00
|
|
|
closureNode.Chunk,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
op := cc.closure(closureNode)
|
2015-01-22 01:06:14 +08:00
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.pushVar(varName, callableType{})
|
2015-01-22 01:06:14 +08:00
|
|
|
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
|
|
|
ec.local[varName] = newInternalVariable(op.f(ec)[0], callableType{})
|
2015-01-22 06:43:30 +08:00
|
|
|
return success
|
2015-01-22 01:06:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 04:59:14 +08:00
|
|
|
func maybeClosurePrimary(cn *parse.Compound) (*parse.Closure, bool) {
|
2015-02-10 18:43:01 +08:00
|
|
|
if len(cn.Nodes) == 1 && cn.Nodes[0].Right == nil && cn.Nodes[0].Left.Typ == parse.ClosurePrimary {
|
2015-02-25 04:59:14 +08:00
|
|
|
return cn.Nodes[0].Left.Node.(*parse.Closure), true
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-02-25 04:59:14 +08:00
|
|
|
func maybeStringPrimary(cn *parse.Compound) (string, bool) {
|
2015-02-10 18:43:01 +08:00
|
|
|
if len(cn.Nodes) == 1 && cn.Nodes[0].Right == nil && cn.Nodes[0].Left.Typ == parse.StringPrimary {
|
2015-02-25 04:59:14 +08:00
|
|
|
return cn.Nodes[0].Left.Node.(*parse.String).Text, true
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
type ifBranch struct {
|
|
|
|
condition valuesOp
|
|
|
|
body valuesOp
|
|
|
|
}
|
|
|
|
|
|
|
|
// IfForm = 'if' Branch { 'else' 'if' Branch } [ 'else' Branch ]
|
|
|
|
// Branch = SpacedNode.condition ClosurePrimary.body
|
|
|
|
//
|
|
|
|
// The condition part of a Branch ends as soon as a Compound of a single
|
|
|
|
// ClosurePrimary is encountered.
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileIf(cc *compileCtx, fn *parse.Form) exitusOp {
|
2015-02-10 18:43:01 +08:00
|
|
|
compounds := fn.Args.Nodes
|
|
|
|
var branches []*ifBranch
|
|
|
|
|
|
|
|
nextBranch := func() {
|
2015-02-25 04:59:14 +08:00
|
|
|
var conds []*parse.Compound
|
2015-02-10 18:43:01 +08:00
|
|
|
for i, cn := range compounds {
|
|
|
|
if closure, ok := maybeClosurePrimary(cn); ok {
|
|
|
|
if i == 0 {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(cn.Pos, "expect condition")
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
2015-02-25 22:04:10 +08:00
|
|
|
condition := cc.compounds(conds)
|
2015-02-10 18:43:01 +08:00
|
|
|
if closure.ArgNames != nil && len(closure.ArgNames.Nodes) > 0 {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(closure.ArgNames.Pos, "unexpected arguments")
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
2015-02-25 22:04:10 +08:00
|
|
|
body := cc.closure(closure)
|
2015-02-10 18:43:01 +08:00
|
|
|
branches = append(branches, &ifBranch{condition, body})
|
|
|
|
compounds = compounds[i+1:]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conds = append(conds, cn)
|
|
|
|
}
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(compounds[len(compounds)-1].Pos, "expect body after this")
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
|
|
|
// if branch
|
|
|
|
nextBranch()
|
|
|
|
// else-if branches
|
|
|
|
for len(compounds) >= 2 {
|
|
|
|
s1, _ := maybeStringPrimary(compounds[0])
|
|
|
|
s2, _ := maybeStringPrimary(compounds[1])
|
|
|
|
if s1 == "else" && s2 == "if" {
|
|
|
|
compounds = compounds[2:]
|
|
|
|
nextBranch()
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else branch
|
|
|
|
if len(compounds) > 0 {
|
|
|
|
s, _ := maybeStringPrimary(compounds[0])
|
|
|
|
if s == "else" {
|
|
|
|
if len(compounds) == 1 {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(compounds[0].Pos, "expect body after this")
|
2015-02-10 18:43:01 +08:00
|
|
|
} else if len(compounds) > 2 {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(compounds[2].Pos, "trailing garbage")
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
|
|
|
body, ok := maybeClosurePrimary(compounds[1])
|
|
|
|
if !ok {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(compounds[1].Pos, "expect body")
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
|
|
|
branches = append(branches, &ifBranch{
|
2015-02-25 22:04:10 +08:00
|
|
|
literalValue(boolean(true)), cc.closure(body)})
|
2015-02-10 18:43:01 +08:00
|
|
|
} else {
|
2015-02-25 22:04:10 +08:00
|
|
|
cc.errorf(compounds[0].Pos, "trailing garbage")
|
2015-02-10 18:43:01 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
2015-02-10 18:43:01 +08:00
|
|
|
for _, ib := range branches {
|
2015-02-25 19:21:06 +08:00
|
|
|
if allTrue(ib.condition.f(ec)) {
|
|
|
|
f := ib.body.f(ec)[0].(*closure)
|
|
|
|
su := f.Exec(ec.copy("closure of if"), []Value{})
|
2015-02-10 18:43:01 +08:00
|
|
|
for _ = range su {
|
|
|
|
}
|
|
|
|
// TODO(xiaq): Return the exitus of the body
|
|
|
|
return success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:04:10 +08:00
|
|
|
func compileStaticTypeof(cc *compileCtx, fn *parse.Form) exitusOp {
|
2015-01-20 05:11:44 +08:00
|
|
|
// Do conventional compiling of all compounds, only keeping the static type
|
|
|
|
// information
|
|
|
|
var trs []typeRun
|
|
|
|
for _, cn := range fn.Args.Nodes {
|
2015-02-25 22:04:10 +08:00
|
|
|
trs = append(trs, cc.compound(cn).tr)
|
2015-01-20 05:11:44 +08:00
|
|
|
}
|
2015-02-25 19:21:06 +08:00
|
|
|
return func(ec *evalCtx) exitus {
|
|
|
|
out := ec.ports[1].ch
|
2015-01-20 05:11:44 +08:00
|
|
|
for _, tr := range trs {
|
2015-01-27 02:05:07 +08:00
|
|
|
out <- str(tr.String())
|
2015-01-20 05:11:44 +08:00
|
|
|
}
|
2015-01-22 06:43:30 +08:00
|
|
|
return success
|
2015-01-20 05:11:44 +08:00
|
|
|
}
|
|
|
|
}
|