colourise the terminal

This commit is contained in:
2023-03-08 19:04:53 +00:00
parent 11ed803601
commit f7603e30c4
18 changed files with 373 additions and 76 deletions

View File

@@ -1,10 +1,7 @@
package main package main
import ( import (
"bufio"
"fmt"
"math/big" "math/big"
"os"
) )
type builtinFunc struct { type builtinFunc struct {
@@ -12,22 +9,12 @@ type builtinFunc struct {
FUNC func(...any) (any, ArErr) FUNC func(...any) (any, ArErr)
} }
func ArgonMult(args ...any) (any, ArErr) { func ArgonString(args ...any) (any, ArErr) {
return reduce(func(x any, y any) any { return anyToArgon(args[0], true, false, 3, 0, false, 0), ArErr{}
return newNumber().Mul(y.(number), x.(number))
}, args), ArErr{}
} }
func ArgonInput(args ...any) (any, ArErr) { func ArgonInput(args ...any) (any, ArErr) {
output := []any{} return input(args...), ArErr{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0))
}
fmt.Print(output...)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
return input, ArErr{}
} }
func ArgonNumber(args ...any) (any, ArErr) { func ArgonNumber(args ...any) (any, ArErr) {

View File

@@ -3,14 +3,15 @@ package main
var vars = scope{} var vars = scope{}
func init() { func init() {
vars["window"] = vars vars["vars"] = vars
vars["term"] = ArTerm vars["term"] = ArTerm
vars["true"] = true vars["true"] = true
vars["false"] = false vars["false"] = false
vars["null"] = nil vars["null"] = nil
vars["input"] = builtinFunc{"input", ArgonInput} vars["input"] = builtinFunc{"input", ArgonInput}
vars["number"] = builtinFunc{"number", ArgonNumber} vars["number"] = builtinFunc{"number", ArgonNumber}
vars["mult"] = builtinFunc{"mult", ArgonMult} vars["string"] = builtinFunc{"string", ArgonString}
vars["infinity"] = infinity
vars["length"] = builtinFunc{"length", func(a ...any) (any, ArErr) { vars["length"] = builtinFunc{"length", func(a ...any) (any, ArErr) {
switch x := a[0].(type) { switch x := a[0].(type) {
case string: case string:

View File

@@ -4,7 +4,7 @@ import (
"strings" "strings"
) )
var commentCompile = makeRegex("(.|\n)*//(.|\n)*") var commentCompile = makeRegex("(.)*#(.)*")
func isComment(code UNPARSEcode) bool { func isComment(code UNPARSEcode) bool {
return commentCompile.MatchString(code.code) return commentCompile.MatchString(code.code)
@@ -15,11 +15,11 @@ func isBlank(code UNPARSEcode) bool {
} }
func parseComment(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr) { func parseComment(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr) {
split := strings.Split(code.code, "//") split := strings.Split(code.code, "#")
temp := []string{} temp := []string{}
for i := 0; i < len(split)-1; i++ { for i := 0; i < len(split)-1; i++ {
temp = append(temp, split[i]) temp = append(temp, split[i])
joined := strings.Join(temp, "//") joined := strings.Join(temp, "#")
resp, worked, _, _ := translateVal(UNPARSEcode{code: joined, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, true) resp, worked, _, _ := translateVal(UNPARSEcode{code: joined, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, true)
if worked { if worked {
return resp, true, ArErr{} return resp, true, ArErr{}

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"os"
) )
type ArErr struct { type ArErr struct {
@@ -20,6 +19,5 @@ func panicErr(err ArErr) {
fmt.Println(" " + err.code) fmt.Println(" " + err.code)
fmt.Println() fmt.Println()
} }
fmt.Println(err.TYPE+":", err.message) fmt.Printf("\x1b[%dm%s\x1b[0m", 91, fmt.Sprint(err.TYPE, ": ", err.message, "\n"))
os.Exit(1)
} }

View File

@@ -99,7 +99,7 @@ func importMod(realpath string, origin string, main bool) ArErr {
return translationerr return translationerr
} }
global := scope{} global := scope{}
_, runimeErr := run(translated, stack{vars, global}) _, runimeErr, _, _ := run(translated, stack{vars, global})
if runimeErr.EXISTS { if runimeErr.EXISTS {
return runimeErr return runimeErr
} }

19
src/input.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"bufio"
"fmt"
"os"
)
func input(args ...any) string {
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, true, 0))
}
fmt.Print(output...)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
return input
}

View File

@@ -16,10 +16,12 @@ func main() {
panic(e) panic(e)
} }
if len(Args) == 0 { if len(Args) == 0 {
panic("No file specified") shell()
os.Exit(0)
} }
err := importMod(Args[0], ex, true) err := importMod(Args[0], ex, true)
if err.EXISTS { if err.EXISTS {
panicErr(err) panicErr(err)
os.Exit(1)
} }
} }

View File

@@ -45,6 +45,7 @@ func mapGet(r ArMapGet, stack stack) (any, ArErr) {
} }
return m[key], ArErr{} return m[key], ArErr{}
case ArClass: case ArClass:
fmt.Println(m.MAP)
if _, ok := m.MAP[key]; !ok { if _, ok := m.MAP[key]; !ok {
return nil, ArErr{ return nil, ArErr{
"KeyError", "KeyError",
@@ -60,7 +61,7 @@ func mapGet(r ArMapGet, stack stack) (any, ArErr) {
} }
return nil, ArErr{ return nil, ArErr{
"TypeError", "TypeError",
"cannot read " + anyToArgon(key, true, true, 3, 0) + " from type '" + typeof(resp) + "'", "cannot read " + anyToArgon(key, true, true, 3, 0, false, 0) + " from type '" + typeof(resp) + "'",
r.line, r.line,
r.path, r.path,
r.code, r.code,

View File

@@ -2,3 +2,4 @@ package main
var PI, _ = newNumber().SetString("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989") var PI, _ = newNumber().SetString("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989")
var e, _ = newNumber().SetString("2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509961818815930416903515988885193458072738667385894228792284998920868058257492796104841984443634632449684875602336248270419786232090021609902353043699418491463140934317381436405462531520961836908887070167683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350354") var e, _ = newNumber().SetString("2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190115738341879307021540891499348841675092447614606680822648001684774118537423454424371075390777449920695517027618386062613313845830007520449338265602976067371132007093287091274437470472306969772093101416928368190255151086574637721112523897844250569536967707854499699679468644549059879316368892300987931277361782154249992295763514822082698951936680331825288693984964651058209392398294887933203625094431173012381970684161403970198376793206832823764648042953118023287825098194558153017567173613320698112509961818815930416903515988885193458072738667385894228792284998920868058257492796104841984443634632449684875602336248270419786232090021609902353043699418491463140934317381436405462531520961836908887070167683964243781405927145635490613031072085103837505101157477041718986106873969655212671546889570350354")
var infinity, _ = newNumber().SetString("1e1000")

View File

@@ -58,6 +58,7 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
for i := 0; i < len(operations); i++ { for i := 0; i < len(operations); i++ {
values := []any{} values := []any{}
current := 0 current := 0
totalindex := 1
for l := 0; l < len(code.code); l++ { for l := 0; l < len(code.code); l++ {
for j := 0; j < len(operations[i]); j++ { for j := 0; j < len(operations[i]); j++ {
if len(code.code[l:]) >= len(operations[i][j]) && code.code[l:l+len(operations[i][j])] == operations[i][j] { if len(code.code[l:]) >= len(operations[i][j]) && code.code[l:l+len(operations[i][j])] == operations[i][j] {
@@ -71,7 +72,7 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
}, index, codelines, false) }, index, codelines, false)
if success { if success {
index += respindex - 1 totalindex += respindex - 1
values = append(values, resp) values = append(values, resp)
current = l + len(operations[i][j]) current = l + len(operations[i][j])
} }
@@ -87,7 +88,7 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
path: code.path, path: code.path,
}, index, codelines, false) }, index, codelines, false)
if success { if success {
index += respindex - 1 totalindex += respindex - 1
values = append(values, resp) values = append(values, resp)
return operationType{ return operationType{
i, i,
@@ -95,12 +96,12 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
code.line, code.line,
code.realcode, code.realcode,
code.path, code.path,
}, true, err, index }, true, err, totalindex
} }
return operationType{}, false, err, index return operationType{}, false, err, totalindex
} }
} }
return operationType{}, false, ArErr{}, index return operationType{}, false, ArErr{}, 0
} }
func compareValues(o operationType, stack stack) (bool, ArErr) { func compareValues(o operationType, stack stack) (bool, ArErr) {
@@ -216,7 +217,7 @@ func calcNegative(o operationType, stack stack) (number, ArErr) {
true, true,
} }
} }
output := resp.(number) output := newNumber().Set(resp.(number))
for i := 1; i < len(o.values); i++ { for i := 1; i < len(o.values); i++ {
resp, err := runVal( resp, err := runVal(
o.values[i], o.values[i],
@@ -242,6 +243,52 @@ func calcNegative(o operationType, stack stack) (number, ArErr) {
return output, ArErr{} return output, ArErr{}
} }
func calcDiv(o operationType, stack stack) (number, ArErr) {
resp, err := runVal(
o.values[0],
stack,
)
resp = classVal(resp)
if err.EXISTS {
return nil, err
}
if !isAnyNumber(resp) {
return nil, ArErr{
"Runtime Error",
"Cannot subtract from type '" + typeof(resp) + "'",
o.line,
o.path,
o.code,
true,
}
}
output := newNumber().Set(resp.(number))
for i := 1; i < len(o.values); i++ {
resp, err := runVal(
o.values[i],
stack,
)
resp = classVal(resp)
if err.EXISTS {
return nil, err
}
if typeof(resp) == "number" {
output = output.Quo(output, resp.(number))
} else {
return nil, ArErr{
"Runtime Error",
"Cannot divide type '" + typeof(resp) + "'",
o.line,
o.path,
o.code,
true,
}
}
}
return output, ArErr{}
}
func calcAdd(o operationType, stack stack) (any, ArErr) { func calcAdd(o operationType, stack stack) (any, ArErr) {
resp, err := runVal( resp, err := runVal(
@@ -254,7 +301,9 @@ func calcAdd(o operationType, stack stack) (any, ArErr) {
} }
var output any = resp var output any = resp
if typeof(output) != "number" { if typeof(output) != "number" {
output = anyToArgon(resp, false, true, 3, 0) output = anyToArgon(resp, false, true, 3, 0, false, 0)
} else {
output = newNumber().Set(output.(number))
} }
for i := 1; i < len(o.values); i++ { for i := 1; i < len(o.values); i++ {
resp, err := runVal( resp, err := runVal(
@@ -266,18 +315,65 @@ func calcAdd(o operationType, stack stack) (any, ArErr) {
return nil, err return nil, err
} }
if typeof(output) == "number" && typeof(resp) == "string" { if typeof(output) == "number" && typeof(resp) == "string" {
output = anyToArgon(output, false, true, 3, 0) output = anyToArgon(output, false, true, 3, 0, false, 0)
} }
if typeof(output) == "number" { if typeof(output) == "number" {
output = newNumber().Add(output.(number), resp.(number)) output = output.(number).Add(output.(number), resp.(number))
} else { } else {
output = output.(string) + anyToArgon(resp, false, true, 3, 0) output = output.(string) + anyToArgon(resp, false, true, 3, 0, false, 0)
} }
} }
return output, ArErr{} return output, ArErr{}
} }
func calcMul(o operationType, stack stack) (any, ArErr) {
resp, err := runVal(
o.values[0],
stack,
)
resp = classVal(resp)
if err.EXISTS {
return nil, err
}
var output any = resp
if typeof(output) != "number" {
output = anyToArgon(resp, false, true, 3, 0, false, 0)
} else {
output = newNumber().Set(output.(number))
}
for i := 1; i < len(o.values); i++ {
resp, err := runVal(
o.values[i],
stack,
)
resp = classVal(resp)
if err.EXISTS {
return nil, err
}
if typeof(output) == "number" && typeof(resp) == "string" {
output = anyToArgon(output, false, true, 3, 0, false, 0)
}
if typeof(output) == "number" {
output = output.(number).Mul(output.(number), resp.(number))
} else if typeof(resp) == "number" {
n, _ := resp.(number).Float64()
output = strings.Repeat(output.(string), int(n))
} else {
return nil, ArErr{
"Runtime Error",
"Cannot multiply type '" + typeof(resp) + "'",
o.line,
o.path,
o.code,
true,
}
}
}
return output, ArErr{}
}
func calcAnd(o operationType, stack stack) (any, ArErr) { func calcAnd(o operationType, stack stack) (any, ArErr) {
var output any = false var output any = false
for i := 0; i < len(o.values); i++ { for i := 0; i < len(o.values); i++ {
@@ -356,7 +452,7 @@ func calcIn(o operationType, stack stack) (bool, ArErr) {
switch x := resp2.(type) { switch x := resp2.(type) {
case string: case string:
check := anyToArgon(resp, false, true, 3, 0) check := anyToArgon(resp, false, true, 3, 0, false, 0)
return strings.Contains(x, check), ArErr{} return strings.Contains(x, check), ArErr{}
case []any: case []any:
return stringInSlice(resp, x), ArErr{} return stringInSlice(resp, x), ArErr{}
@@ -379,7 +475,7 @@ func equals(a any, b any) bool {
if typeof(a) == "number" && typeof(b) == "number" { if typeof(a) == "number" && typeof(b) == "number" {
return a.(number).Cmp(b.(number)) == 0 return a.(number).Cmp(b.(number)) == 0
} else if typeof(a) == "string" || typeof(b) == "string" { } else if typeof(a) == "string" || typeof(b) == "string" {
return anyToArgon(a, false, true, 3, 0) == anyToArgon(b, false, true, 3, 0) return anyToArgon(a, false, false, 3, 0, false, 0) == anyToArgon(b, false, false, 3, 0, false, 0)
} }
return reflect.DeepEqual(a, b) return reflect.DeepEqual(a, b)
} }
@@ -412,6 +508,10 @@ func runOperation(o operationType, stack stack) (any, ArErr) {
return calcAdd(o, stack) return calcAdd(o, stack)
case 11: case 11:
return calcNegative(o, stack) return calcNegative(o, stack)
case 12:
return calcMul(o, stack)
case 13:
return calcDiv(o, stack)
} }
panic("Unknown operation: " + fmt.Sprint(o.operation)) panic("Unknown operation: " + fmt.Sprint(o.operation))

View File

@@ -54,12 +54,15 @@ func runVal(line any, stack stack) (any, ArErr) {
} }
// returns error // returns error
func run(translated []any, stack stack) (any, ArErr) { func run(translated []any, stack stack) (any, ArErr, int, any) {
var output any = nil
count := 0
for _, val := range translated { for _, val := range translated {
_, err := runVal(val, stack) val, err := runVal(val, stack)
output = val
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err, count, output
} }
} }
return nil, ArErr{} return nil, ArErr{}, count, output
} }

36
src/shell.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"fmt"
"os"
"os/signal"
)
func shell() {
global := stack{vars, scope{}}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
if sig == os.Interrupt {
fmt.Println("\x1b[0m\n\x1b[32;5;240mBye :)\x1b[0m")
os.Exit(0)
}
}
}()
for {
code := input("\x1b[38;5;240m>>> \x1b[0m\x1b[1;5;240m")
fmt.Print("\x1b[0m")
translated, translationerr := translate([]UNPARSEcode{{code, code, 1, "<shell>"}})
if translationerr.EXISTS {
panicErr(translationerr)
}
_, runimeErr, count, output := run(translated, global)
if runimeErr.EXISTS {
panicErr(runimeErr)
}
if count == 0 {
fmt.Println(anyToArgon(output, true, true, 3, 0, true, 1))
}
}
}

View File

@@ -11,6 +11,15 @@ func isString(code UNPARSEcode) bool {
return stringCompile.MatchString(code.code) return stringCompile.MatchString(code.code)
} }
func toStringClass(str string) ArClass {
return ArClass{
value: str,
MAP: ArMap{
"length": len(str),
},
}
}
func unquoted( func unquoted(
str string, str string,
) (string, error) { ) (string, error) {
@@ -22,7 +31,12 @@ func unquoted(
str = str[1 : len(str)-1] str = str[1 : len(str)-1]
str = strings.Replace(str, "\\'", "'", -1) str = strings.Replace(str, "\\'", "'", -1)
str = "\"" + str + "\"" str = "\"" + str + "\""
return strconv.Unquote(str) output, err := strconv.Unquote(str)
if err != nil {
return "", err
}
classoutput := (output)
return classoutput, nil
} }
// returns translateString, success, error // returns translateString, success, error

View File

@@ -1,16 +1,98 @@
package main package main
import "fmt" import (
"fmt"
"time"
)
func ArgonLog(args ...any) (any, ArErr) { var timing = ArMap{}
var plain = ArMap{
"log": builtinFunc{"log", func(args ...any) (any, ArErr) {
output := []any{} output := []any{}
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0)) output = append(output, anyToArgon(args[i], false, true, 3, 0, false, 0))
} }
fmt.Println(output...) fmt.Println(output...)
return nil, ArErr{} return nil, ArErr{}
}},
"logVal": builtinFunc{"logVal", func(args ...any) (any, ArErr) {
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], true, true, 3, 0, false, 0))
}
fmt.Println(output...)
return nil, ArErr{}
}},
} }
var ArTerm = ArMap{ var ArTerm = ArMap{
"log": builtinFunc{"log", ArgonLog}, "log": builtinFunc{"log", func(args ...any) (any, ArErr) {
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, true, 1))
}
fmt.Println(output...)
return nil, ArErr{}
}},
"logVal": builtinFunc{"logVal", func(args ...any) (any, ArErr) {
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], true, true, 3, 0, true, 1))
}
fmt.Println(output...)
return nil, ArErr{}
}},
"print": builtinFunc{"print", func(args ...any) (any, ArErr) {
output := []any{}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, false, 3, 0, false, 1))
}
fmt.Println(output...)
return nil, ArErr{}
}},
"plain": plain,
"error": builtinFunc{"error", func(args ...any) (any, ArErr) {
output := []any{"error: "}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, false, 0))
}
fmt.Printf("\x1b[%dm%s\x1b[0m", 91, fmt.Sprint(output...)+"\n")
return nil, ArErr{}
},
},
"warn": builtinFunc{"error", func(args ...any) (any, ArErr) {
output := []any{"warning: "}
for i := 0; i < len(args); i++ {
output = append(output, anyToArgon(args[i], false, true, 3, 0, false, 0))
}
fmt.Printf("\x1b[%dm%s\x1b[0m", 93, fmt.Sprint(output...)+"\n")
return nil, ArErr{}
},
},
"time": builtinFunc{"time", func(args ...any) (any, ArErr) {
var id any = nil
if len(args) > 0 {
id = args[0]
}
timing[id] = time.Now()
return nil, ArErr{}
},
},
"timeEnd": builtinFunc{"timeEnd", func(args ...any) (any, ArErr) {
var id any = nil
if len(args) > 0 {
id = args[0]
}
if _, ok := timing[id]; !ok {
return nil, ArErr{TYPE: "TypeError", message: "Cannot find timer with id '" + fmt.Sprint(id) + "'", EXISTS: true}
}
timesince := time.Since(timing[id].(time.Time))
delete(timing, id)
if id == nil {
id = "Timer"
}
fmt.Printf("\x1b[%dm%s\x1b[0m", 34, fmt.Sprint(anyToArgon(id, false, true, 3, 0, false, 0), ": ", timesince)+"\n")
return nil, ArErr{}
}},
} }

View File

@@ -7,34 +7,68 @@ import (
"strings" "strings"
) )
func anyToArgon(x any, quote bool, simplify bool, depth int, indent int) string { func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, color bool, plain int) string {
output := []string{}
maybenewline := ""
if plain == 1 {
maybenewline = "\n"
}
if depth == 0 { if depth == 0 {
return "(...)" if color {
output = append(output, "\x1b[38;5;240m")
}
output = append(output, "(...)")
if color {
output = append(output, "\x1b[0m")
}
return strings.Join(output, "")
} }
switch x := x.(type) { switch x := x.(type) {
case string: case string:
if !quote { if !quote {
return x output = append(output, x)
break
}
if color {
output = append(output, "\x1b[33;5;240m")
}
output = append(output, strconv.Quote(x))
if color {
output = append(output, "\x1b[0m")
} }
return strconv.Quote(x)
case number: case number:
if color {
output = append(output, "\x1b[34;5;240m")
}
num, _ := x.Float64() num, _ := x.Float64()
if math.IsNaN(num) { if math.IsNaN(num) {
return "NaN" output = append(output, "NaN")
} else if math.IsInf(num, 1) { } else if math.IsInf(num, 1) {
return "infinity" output = append(output, "infinity")
} else if math.IsInf(num, -1) { } else if math.IsInf(num, -1) {
return "-infinity" output = append(output, "-infinity")
} else { } else {
if simplify { output = append(output, numberToString(x, 0, simplify))
return numberToString(x, 0, true)
} }
return numberToString(x, 0, false) if color {
output = append(output, "\x1b[0m")
} }
case bool: case bool:
return strconv.FormatBool(x) if color {
output = append(output, "\x1b[35;5;240m")
}
output = append(output, strconv.FormatBool(x))
if color {
output = append(output, "\x1b[0m")
}
case nil: case nil:
return "null" if color {
output = append(output, "\x1b[31;5;240m")
}
output = append(output, "null")
if color {
output = append(output, "\x1b[0m")
}
case ArMap: case ArMap:
keys := make([]any, len(x)) keys := make([]any, len(x))
@@ -45,16 +79,29 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int) string
} }
output := []string{} output := []string{}
for _, key := range keys { for _, key := range keys {
output = append(output, anyToArgon(key, true, true, depth, indent+1)+": "+anyToArgon(x[key], true, true, depth-1, indent+1)) output = append(output, anyToArgon(key, true, true, depth, (indent+1)*plain, color, plain)+": "+anyToArgon(x[key], true, true, depth-1, indent+1, color, plain))
} }
return "{\n" + (strings.Repeat(" ", indent+1)) + strings.Join(output, ",\n"+(strings.Repeat(" ", indent+1))) + "\n" + (strings.Repeat(" ", indent)) + "}" return "{" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + "}"
case builtinFunc: case builtinFunc:
return "<builtin function " + x.name + ">" if color {
output = append(output, "\x1b[38;5;240m")
}
output = append(output, "<builtin function "+x.name+">")
if color {
output = append(output, "\x1b[0m")
}
case Callable: case Callable:
return "<function " + x.name + ">" if color {
output = append(output, "\x1b[38;5;240m")
}
output = append(output, "<function "+x.name+">")
if color {
output = append(output, "\x1b[0m")
}
case ArClass: case ArClass:
return anyToArgon(x.value, false, true, depth-1, indent+1) return anyToArgon(x.value, quote, simplify, depth, indent, color, plain)
default: default:
return fmt.Sprint(x) return fmt.Sprint(x)
} }
return strings.Join(output, "")
} }

View File

@@ -26,6 +26,11 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine b
if worked { if worked {
return bracket, worked, err, step return bracket, worked, err, step
} }
} else if isSetVariable(code) {
setvar, worked, err, step := parseSetVariable(code, index, codelines)
if worked {
return setvar, worked, err, step
}
} }
operation, worked, err, step := parseOperations(code, index, codelines) operation, worked, err, step := parseOperations(code, index, codelines)
if worked { if worked {
@@ -33,9 +38,7 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine b
} else if err.EXISTS { } else if err.EXISTS {
return nil, worked, err, step return nil, worked, err, step
} }
if isSetVariable(code) { if isNumber(code) {
return parseSetVariable(code, index, codelines)
} else if isNumber(code) {
return parseNumber(code) return parseNumber(code)
} else if isNegative(code) { } else if isNegative(code) {
return parseNegative(code, index, codelines) return parseNegative(code, index, codelines)

View File

@@ -49,7 +49,7 @@ func parseVariable(code UNPARSEcode) (accessVariable, bool, ArErr, int) {
if blockedVariableNames[name] { if blockedVariableNames[name] {
return accessVariable{}, false, ArErr{"Naming Error", "Naming Error: \"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1 return accessVariable{}, false, ArErr{"Naming Error", "Naming Error: \"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1
} }
return accessVariable{name: name, code: code.code, line: code.line}, true, ArErr{}, 1 return accessVariable{name: name, code: code.code, line: code.line, path: code.path}, true, ArErr{}, 1
} }
func readVariable(v accessVariable, stack stack) (any, ArErr) { func readVariable(v accessVariable, stack stack) (any, ArErr) {

View File

@@ -1 +1,4 @@
term.log(sqrt(10)) x = 10
term.log(x)
x = x+1
term.log(x)