From 5f12fd1d87122e0bd457393f5b97355c6b2c2913 Mon Sep 17 00:00:00 2001 From: rexim Date: Mon, 7 Oct 2024 06:06:58 +0700 Subject: [PATCH] Move `over` and `dup` to the global funcs array --- markut.go | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/markut.go b/markut.go index 6435625..8f55513 100644 --- a/markut.go +++ b/markut.go @@ -502,6 +502,37 @@ var funcs = map[string]Func{ return true; }, }, + "over": { + Description: "Copy the argument below the top fo the stack on top", + Signature: " -- ", + Category: "Stack", + Run: func(context *EvalContext, command string, token Token) bool { + arity := 2 + if len(context.argsStack) < arity { + fmt.Printf("%s: Expected %d arguments but got %d", token.Loc, arity, len(context.argsStack)); + return false; + } + n := len(context.argsStack) + context.argsStack = append(context.argsStack, context.argsStack[n-2]); + return true; + }, + }, + "dup": { + Description: "Duplicate the argument on top of the stack", + Signature: " -- ", + Category: "Stack", + Run: func(context *EvalContext, command string, token Token) bool { + arity := 1 + if len(context.argsStack) < arity { + fmt.Printf("%s: Expected %d arguments but got %d", token.Loc, arity, len(context.argsStack)); + return false; + } + n := len(context.argsStack) + // TODO: the location of the dupped value should be the location of the "dup" token + context.argsStack = append(context.argsStack, context.argsStack[n-1]) + return true + }, + }, } // This function is compatible with the format https://www.twitchchatdownloader.com/ generates. @@ -628,23 +659,6 @@ func (context *EvalContext) evalMarkutFile(path string) bool { return false } context.inputPath = string(path.Text) - case "over": - arity := 2 - if len(context.argsStack) < arity { - fmt.Printf("%s: Expected %d arguments but got %d", token.Loc, arity, len(context.argsStack)); - return false; - } - n := len(context.argsStack) - context.argsStack = append(context.argsStack, context.argsStack[n-2]); - case "dup": - arity := 1 - if len(context.argsStack) < arity { - fmt.Printf("%s: Expected %d arguments but got %d", token.Loc, arity, len(context.argsStack)); - return false; - } - n := len(context.argsStack) - // TODO: the location of the dupped value should be the location of the "dup" token - context.argsStack = append(context.argsStack, context.argsStack[n-1]) case "chapter": args, err = context.typeCheckArgs(token.Loc, TokenString, TokenTimestamp) if err != nil {