From 75358e5d3530be3e216b4c7f1ba108c93f93d48c Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Sun, 2 Jun 2024 12:32:08 +0200 Subject: [PATCH] func-fmt.go: print() and println() --- func-fmt.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 func-fmt.go diff --git a/func-fmt.go b/func-fmt.go new file mode 100644 index 0000000..5644c3d --- /dev/null +++ b/func-fmt.go @@ -0,0 +1,36 @@ +// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). +// All rights reserved. + +// func-fmt.go +package expr + +import "fmt" + +func printFunc(ctx ExprContext, name string, args []any) (result any, err error) { + var n int + if n, err = fmt.Print(args...); err == nil { + result = int64(n) + } + return +} + +func printLnFunc(ctx ExprContext, name string, args []any) (result any, err error) { + var n int + if n, err = fmt.Println(args...); err == nil { + result = int64(n) + } + return +} + +func ImportFmtFuncs(ctx ExprContext) { + ctx.RegisterFunc("print", newGolangFunctor(printFunc), typeInt, []ExprFuncParam{ + newFuncParamFlag(paramItem, pfRepeat), + }) + ctx.RegisterFunc("println", newGolangFunctor(printLnFunc), typeInt, []ExprFuncParam{ + newFuncParamFlag(paramItem, pfRepeat), + }) +} + +func init() { + registerImport("fmt", ImportFmtFuncs, "String and console formatting functions") +}