From 68bf5c2e5a75fef56af60b325d1ae11cf6cf6213 Mon Sep 17 00:00:00 2001 From: refaktor Date: Mon, 1 Apr 2024 22:57:44 +0200 Subject: [PATCH] small fixes to builins, embed, fn\cc (closure) --- evaldo/builtins.go | 47 +++++++++++++++++++++++++++++++++----------- examples/99beers.rye | 23 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 examples/99beers.rye diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 60227b6e..9e37be8b 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -1345,47 +1345,47 @@ var builtins = map[string]*env.Builtin{ return arg0 }, }, - "prnv": { // ** + "embed": { // ** Argsn: 2, - Doc: "Prints a value.", + Doc: "Embeds a value in string.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch arg := arg1.(type) { case env.String: vals := arg0.Print(*ps.Idx) news := strings.ReplaceAll(arg.Value, "{}", vals) - fmt.Print(news) + return *env.NewString(news) default: - fmt.Print(arg0.Print(*ps.Idx)) + return MakeArgError(ps, 1, []env.Type{env.StringType}, "esc") } return arg0 }, }, - "printv": { // ** + "prnv": { // ** Argsn: 2, - Doc: "Prints a value and adds a newline.", + Doc: "Prints a value.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch arg := arg1.(type) { case env.String: vals := arg0.Print(*ps.Idx) news := strings.ReplaceAll(arg.Value, "{}", vals) - fmt.Println(news) + fmt.Print(news) default: - fmt.Println(arg0.Print(*ps.Idx)) + return MakeArgError(ps, 1, []env.Type{env.StringType}, "esc") } return arg0 }, }, - "print\\val": { // DEPRECATED ... too visually noisy ... new is printv and accepts just {} + "printv": { // ** Argsn: 2, Doc: "Prints a value and adds a newline.", Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch arg := arg1.(type) { case env.String: vals := arg0.Print(*ps.Idx) - news := strings.ReplaceAll(arg.Value, "{{}}", vals) + news := strings.ReplaceAll(arg.Value, "{}", vals) fmt.Println(news) default: - fmt.Println(arg0.Print(*ps.Idx)) + return MakeArgError(ps, 1, []env.Type{env.StringType}, "esc") } return arg0 }, @@ -4973,6 +4973,31 @@ var builtins = map[string]*env.Builtin{ }, }, + "fn\\cc": { + // a function with context bb: 10 add10 [ a ] context [ b: bb ] [ add a b ] + // add10 [ a ] this [ add a b ] + // later maybe add10 [ a ] [ b: b ] [ add a b ] + // add10 [ a ] [ 'b ] [ add a b ] + Argsn: 2, + Doc: "Creates a function with specific context.", + Pure: true, + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch args := arg0.(type) { + case env.Block: + switch body := arg1.(type) { + case env.Block: + return *env.NewFunctionC(args, body, ps.Ctx, false, false) + default: + ps.ErrorFlag = true + return MakeArgError(ps, 2, []env.Type{env.BlockType}, "fnc") + } + default: + ps.ErrorFlag = true + return MakeArgError(ps, 1, []env.Type{env.BlockType}, "fnc") + } + }, + }, + "fn\\par": { // a function with context bb: 10 add10 [ a ] context [ b: bb ] [ add a b ] // add10 [ a ] this [ add a b ] diff --git a/examples/99beers.rye b/examples/99beers.rye new file mode 100644 index 00000000..3cf38cf4 --- /dev/null +++ b/examples/99beers.rye @@ -0,0 +1,23 @@ + +rye ; Testing with \word instead of |word for pipe-words + +; function private creates a context, executes code in it and just returns the last value +; experimenting with using it for "ultra-private" environments and verbiage :) + +private { + + bottles: fn1 { = 1 \either { "bottle" } { "bottles" } } + verse1: fn\cc { n } { .embed join [ "{} " bottles n " of beer on the wall" ] } + verse2: fn\cc { n } { .embed join [ "{} " bottles n " of beer" ] } + verse3: does { "Take one down and pass it around, " } + + fn\cc { n } { + prn join [ verse1 n ", " verse2 n "." newline verse3 ] + n - 1 :m \either + { verse1 m |+ "." \print , beers m } + { print "no more bottles of beer on the wall." } + } +} :beers + +99 - 90 \beers +