aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-03-03 18:08:04 +0000
committergingerBill <bill@gingerbill.org>2022-03-03 18:08:04 +0000
commita7c0edb046fec9cb43b9846e40c23b5bfc233f84 (patch)
treee794569fe3780fe13837db01d6f1ed1e3e868a51
parent52bb8524b10d39ead50de0f50d3b762a90e7c33a (diff)
Add `int` and `float` functions
-rw-r--r--core/text/template/execute.odin2
-rw-r--r--core/text/template/function.odin58
-rw-r--r--core/text/template/parse/ast.odin1
-rw-r--r--core/text/template/parse/parse.odin1
4 files changed, 58 insertions, 4 deletions
diff --git a/core/text/template/execute.odin b/core/text/template/execute.odin
index c4b1291c3..c20833f5b 100644
--- a/core/text/template/execute.odin
+++ b/core/text/template/execute.odin
@@ -547,7 +547,7 @@ eval_field :: proc(s: ^State, dot: any, ident: string) -> (value: any, err: Erro
rm := (^mem.Raw_Map)(dot.data)
data := uintptr(rm.entries.data)
- for i in 0..<rm.entries.len {
+ for _ in 0..<rm.entries.len {
key: string
switch key_type.id {
case typeid_of(string):
diff --git a/core/text/template/function.odin b/core/text/template/function.odin
index 87958b072..88c5f5730 100644
--- a/core/text/template/function.odin
+++ b/core/text/template/function.odin
@@ -1,8 +1,8 @@
package text_template
-import "core:mem"
import "core:fmt"
import "core:reflect"
+import "core:strconv"
Function :: #type proc(args: []any) -> (value: any, err: Error)
@@ -59,6 +59,62 @@ init_builtin_funcs :: proc() {
return new_any(n), nil
}
+ builtin_funcs["int"] = proc(args: []any) -> (value: any, err: Error) {
+ if len(args) != 1 {
+ err = .Invalid_Argument_Count
+ return
+ }
+ res: i64
+ switch v in get_value(args[0]) {
+ case bool:
+ res = i64(v)
+ case i64:
+ res = i64(v)
+ case f64:
+ res = i64(v)
+ case string:
+ if value, ok := strconv.parse_f64(v); ok {
+ res = i64(value)
+ } else if value, ok := strconv.parse_i64(v); ok {
+ res = i64(value)
+ } else {
+ return nil, .Invalid_Argument_Type
+ }
+ case:
+ return nil, .Invalid_Argument_Type
+ }
+
+ return new_any(res), nil
+ }
+
+ builtin_funcs["float"] = proc(args: []any) -> (value: any, err: Error) {
+ if len(args) != 1 {
+ err = .Invalid_Argument_Count
+ return
+ }
+ res: f64
+ switch v in get_value(args[0]) {
+ case bool:
+ res = f64(i64(v))
+ case i64:
+ res = f64(v)
+ case f64:
+ res = f64(v)
+ case string:
+ if value, ok := strconv.parse_f64(v); ok {
+ res = f64(value)
+ } else if value, ok := strconv.parse_i64(v); ok {
+ res = f64(value)
+ } else {
+ return nil, .Invalid_Argument_Type
+ }
+ case:
+ return nil, .Invalid_Argument_Type
+ }
+
+ return new_any(res), nil
+ }
+
builtin_funcs["print"] = proc(args: []any) -> (value: any, err: Error) {
return new_any(fmt.aprint(..args)), nil
diff --git a/core/text/template/parse/ast.odin b/core/text/template/parse/ast.odin
index 3314b4140..9447e995c 100644
--- a/core/text/template/parse/ast.odin
+++ b/core/text/template/parse/ast.odin
@@ -1,7 +1,6 @@
package text_template_parse
import "../scan"
-import "core:strings"
Pos :: scan.Pos
Token :: scan.Token
diff --git a/core/text/template/parse/parse.odin b/core/text/template/parse/parse.odin
index 40ee12aff..bd1b11b45 100644
--- a/core/text/template/parse/parse.odin
+++ b/core/text/template/parse/parse.odin
@@ -1,6 +1,5 @@
package text_template_parse
-import "core:io"
import "core:fmt"
import "core:mem"
import "core:mem/virtual"