示例#1
0
文件: main.go 项目: raoulvdberge/stak
func setDefaults(scope *parser.Scope) {
	scope.SetSymbol("true", parser.NewSymbol(parser.NewValueFromBool(true), true, false))
	scope.SetSymbol("false", parser.NewSymbol(parser.NewValueFromBool(false), true, false))

	scope.Modules["std"] = parser.NewModule()
	scope.Modules["std"].AddBuiltinFunctions(modules.StdFunctions)

	scope.Modules["os"] = parser.NewModule()
	scope.Modules["os"].AddBuiltinFunctions(modules.OsFunctions)

	scope.Modules["math"] = parser.NewModule()
	scope.Modules["math"].Data["pi"] = parser.NewValueFromFloat64(math.Pi)
	scope.Modules["math"].Data["e"] = parser.NewValueFromFloat64(math.E)
	scope.Modules["math"].AddBuiltinFunctions(modules.MathFunctions)

	scope.Modules["lists"] = parser.NewModule()
	scope.Modules["lists"].AddBuiltinFunctions(modules.ListFunctions)

	scope.Modules["maps"] = parser.NewModule()
	scope.Modules["maps"].AddBuiltinFunctions(modules.MapsFunctions)

	scope.Modules["files"] = parser.NewModule()
	scope.Modules["files"].AddBuiltinFunctions(modules.FilesFunctions)
	scope.Modules["files"].Data["file-separator"] = parser.NewValueFromString(string(os.PathSeparator))

	scope.Modules["strings"] = parser.NewModule()
	scope.Modules["strings"].AddBuiltinFunctions(modules.StringsFunctions)

	scope.Modules["errors"] = parser.NewModule()
	scope.Modules["errors"].AddBuiltinFunctions(modules.ErrorsFunctions)
}
示例#2
0
文件: std.go 项目: raoulvdberge/stak
func orN(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "or-n", parser.VALUE_FUNCTION)

	if err != nil {
		return err
	}

	fakeBlock := parser.NewBlock(nil, nil, nil)

	runErr := values[0].F.Exec(fakeBlock, context.Token)

	if runErr != nil {
		return runErr
	}

	result := false

	if fakeBlock.Stack.Len() >= 1 {
		for fakeBlock.Stack.Len() > 0 {
			value := fakeBlock.Stack.Pop().(*parser.Value)

			if value.Type != parser.VALUE_BOOL {
				return util.NewInvalidArgumentError("or-n", "expected a boolean in the function stack", context.Token.Line, context.Token.Column, context.Token.File)
			}

			result = result || value.B
		}
	} else {
		return util.NewInvalidArgumentError("or-n", "expected at least 2 values to compare in function stack", context.Token.Line, context.Token.Column, context.Token.File)
	}

	context.Block.Stack.Push(parser.NewValueFromBool(result))

	return nil
}
示例#3
0
文件: std.go 项目: raoulvdberge/stak
func le(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "le?", parser.VALUE_NUMBER, parser.VALUE_NUMBER)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(values[1].N.Cmp(values[0].N) <= 0))

	return nil
}
示例#4
0
文件: std.go 项目: raoulvdberge/stak
func ne(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "ne?", parser.VALUE_ANY, parser.VALUE_ANY)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(!values[0].Equals(values[1])))

	return nil
}
示例#5
0
文件: std.go 项目: raoulvdberge/stak
func not(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "not", parser.VALUE_BOOL)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(!values[0].B))

	return nil
}
示例#6
0
文件: std.go 项目: raoulvdberge/stak
func xor(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "xor", parser.VALUE_BOOL, parser.VALUE_BOOL)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(values[0].B || values[1].B && !(values[0].B && values[1].B)))

	return nil
}
示例#7
0
func stringIsUpper(context *parser.FunctionCallContext) error {
	r, err := characterChecks(context, "string-is-upper?")

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(unicode.IsUpper(r)))

	return nil
}
示例#8
0
func stringContains(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "string-contains?", parser.VALUE_STRING, parser.VALUE_STRING)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(strings.Contains(values[1].S, values[0].S)))

	return nil
}
示例#9
0
func fileExists(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "file-exists?", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	exists := true
	if _, err := os.Stat(values[0].S); os.IsNotExist(err) {
		exists = false
	}

	context.Block.Stack.Push(parser.NewValueFromBool(exists))

	return nil
}
示例#10
0
func fileIsDir(context *parser.FunctionCallContext) error {
	values, err := parser.ValidateArguments(context, "file-dir?", parser.VALUE_STRING)

	if err != nil {
		return err
	}

	stat, err := os.Stat(values[0].S)

	if err != nil {
		return err
	}

	context.Block.Stack.Push(parser.NewValueFromBool(stat.IsDir()))

	return nil
}