mirror of
https://github.com/Open-Argon/argon-v3.git
synced 2025-12-06 00:46:07 +00:00
fix brainfuck, allow multiplying array by int, and fix incorrect error message in ord
This commit is contained in:
9
server_test/app.ar
Normal file
9
server_test/app.ar
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import "http.ar" as http
|
||||||
|
|
||||||
|
let server = http.server()
|
||||||
|
|
||||||
|
let home(req,res) = do
|
||||||
|
res.send("hello world")
|
||||||
|
server.get("/", home)
|
||||||
|
|
||||||
|
server.run()
|
||||||
23
src/array.go
23
src/array.go
@@ -61,6 +61,29 @@ func ArArray(arr []any) ArObject {
|
|||||||
return nil, ArErr{}
|
return nil, ArErr{}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
val.obj["__Multiply__"] = builtinFunc{
|
||||||
|
"__Multiply__",
|
||||||
|
func(a ...any) (any, ArErr) {
|
||||||
|
if len(a) != 1 {
|
||||||
|
return nil, ArErr{"Type Error", "expected 1 argument, got " + fmt.Sprint(len(a)), 0, "", "", true}
|
||||||
|
}
|
||||||
|
if typeof(a[0]) != "number" {
|
||||||
|
return nil, ArErr{"Type Error", "cannot multiply array by " + typeof(a[0]), 0, "", "", true}
|
||||||
|
}
|
||||||
|
n := a[0].(number)
|
||||||
|
if !n.IsInt() {
|
||||||
|
return nil, ArErr{"Value Error", "cannot multiply array by float", 0, "", "", true}
|
||||||
|
}
|
||||||
|
if n.Sign() < 0 {
|
||||||
|
return nil, ArErr{"Value Error", "cannot multiply array by negative number", 0, "", "", true}
|
||||||
|
}
|
||||||
|
size := int(n.Num().Int64())
|
||||||
|
retval := make([]any, 0, len(arr)*size)
|
||||||
|
for i := 0; i < size; i++ {
|
||||||
|
retval = append(retval, arr...)
|
||||||
|
}
|
||||||
|
return ArArray(retval), ArErr{}
|
||||||
|
}}
|
||||||
val.obj["__getindex__"] = builtinFunc{
|
val.obj["__getindex__"] = builtinFunc{
|
||||||
"__getindex__",
|
"__getindex__",
|
||||||
func(a ...any) (any, ArErr) {
|
func(a ...any) (any, ArErr) {
|
||||||
|
|||||||
@@ -267,7 +267,7 @@ func makeGlobal() ArObject {
|
|||||||
switch x := a[0].(type) {
|
switch x := a[0].(type) {
|
||||||
case string:
|
case string:
|
||||||
if len(x) != 1 {
|
if len(x) != 1 {
|
||||||
return nil, ArErr{TYPE: "ord", message: "ord takes a string with only one character, got " + fmt.Sprint(len(a)), EXISTS: true}
|
return nil, ArErr{TYPE: "ord", message: "ord takes a string with only one character, got " + fmt.Sprint(len(x)), EXISTS: true}
|
||||||
}
|
}
|
||||||
return floor(newNumber().SetInt64(int64([]rune(x)[0]))), ArErr{}
|
return floor(newNumber().SetInt64(int64([]rune(x)[0]))), ArErr{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,45 @@
|
|||||||
let interpret(code) = do
|
let brainfuck(INPUT) = do
|
||||||
memory = map()
|
memory = [0] * 30*1000
|
||||||
pointer = 0
|
pointer = 0
|
||||||
code_ptr = 0
|
output = ""
|
||||||
loops = []
|
i = 0
|
||||||
|
while (i < INPUT.length) do
|
||||||
|
if (INPUT[i] == ">") do
|
||||||
|
pointer += 1
|
||||||
|
else if (INPUT[i] == "<") do
|
||||||
|
pointer -= 1
|
||||||
|
else if (INPUT[i] == "+") do
|
||||||
|
memory[pointer] += 1
|
||||||
|
else if (INPUT[i] == "-") do
|
||||||
|
memory[pointer] -= 1
|
||||||
|
else if (INPUT[i] == ".") do
|
||||||
|
output += chr(memory[pointer])
|
||||||
|
else if (INPUT[i] == ",") do
|
||||||
|
memory[pointer] = ord(input())
|
||||||
|
else if (INPUT[i] == "[") do
|
||||||
|
if (memory[pointer] == 0) do
|
||||||
|
count = 1
|
||||||
|
while (count > 0) do
|
||||||
|
i += 1
|
||||||
|
if (INPUT[i] == "[") do
|
||||||
|
count += 1
|
||||||
|
else if (INPUT[i] == "]") do
|
||||||
|
count -= 1
|
||||||
|
else if (INPUT[i] == "]") do
|
||||||
|
if (memory[pointer] != 0) do
|
||||||
|
count = 1
|
||||||
|
while (count > 0) do
|
||||||
|
i -= 1
|
||||||
|
if (INPUT[i] == "]") do
|
||||||
|
count += 1
|
||||||
|
else if (INPUT[i] == "[") do
|
||||||
|
count -= 1
|
||||||
|
else do
|
||||||
|
pass
|
||||||
|
i += 1
|
||||||
|
term.print(memory)
|
||||||
|
return output
|
||||||
|
|
||||||
while (code_ptr < code.length) do
|
term.time("brainfuck")
|
||||||
command = code[code_ptr]
|
term.plain.oneLine(brainfuck(input()))
|
||||||
|
term.timeEnd("brainfuck")
|
||||||
if (command == '>') pointer = pointer + 1
|
|
||||||
else if (command == '<') pointer = pointer - 1
|
|
||||||
else if (command == '+') do
|
|
||||||
if (pointer not in memory) memory[pointer] = 0
|
|
||||||
memory[pointer] = memory[pointer] + 1
|
|
||||||
else if (command == '-') do
|
|
||||||
if (pointer not in memory) memory[pointer] = 0
|
|
||||||
memory[pointer] = memory[pointer] - 1
|
|
||||||
else if (command == '.') term.log((memory.get(pointer, 0)))
|
|
||||||
else if (command == ',') memory[pointer] = ord(input())
|
|
||||||
else if (command == '[') do
|
|
||||||
if (memory.get(pointer, 0) == 0) do
|
|
||||||
loop_depth = 1
|
|
||||||
while (loop_depth > 0) do
|
|
||||||
code_ptr = code_ptr + 1
|
|
||||||
if (code[code_ptr] == '[') loop_depth = loop_depth + 1
|
|
||||||
else if (code[code_ptr] == ']') loop_depth = loop_depth - 1
|
|
||||||
else loops.append(code_ptr)
|
|
||||||
else if (command == ']') do
|
|
||||||
if (memory.get(pointer, 0) != 0) code_ptr = loops[-1]
|
|
||||||
else loops.pop()
|
|
||||||
code_ptr = code_ptr + 1
|
|
||||||
|
|
||||||
interpret('>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.>++++++++++.')
|
|
||||||
Reference in New Issue
Block a user