Skip to content

Commit 3ead1de

Browse files
committed
Fix variable declaration syntax
Previous syntax of `boi: varname boi` was objectively wrong. Syntax has been changed to `ONE VARNAME BOI`
1 parent 6cd75f5 commit 3ead1de

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

demo.boi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ bloop < boi:tmp [int 10] boi
4242
boi: tmp [+ boi:tmp [int 1]] boi
4343
BOI
4444

45+
ONE CHANGEY BOI
46+
boi? cat true boi
47+
ONE CHANGEY BOI
48+
boi, "value is " boi:CHANGEY boi
49+
BOI
50+
boi, "value is " boi:CHANGEY boi
51+

functions.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"crypto/rand"
45
"errors"
56
"fmt"
67
)
@@ -75,6 +76,29 @@ func BoiFuncSet(context *BoiContext, args []BoiVar) (BoiVar, error) {
7576
return args[1], nil
7677
}
7778

79+
// BoiFuncDeclare is similar to BoiFuncSet, but does not require a value
80+
// parameter. It instead initializes the variable to a completely random
81+
// value to **ensure** the application programmer can't make assumptions
82+
// about the value. Adding a value parameter anyway is undefined behaviour.
83+
func BoiFuncDeclare(context *BoiContext, args []BoiVar) (BoiVar, error) {
84+
if len(args) < 1 {
85+
return BoiVar{}, errors.New("one requires 1 parameters")
86+
}
87+
key := string(args[0].data)
88+
//context.parentCtx.variables[key] = args[1]
89+
90+
value := make([]byte, 4)
91+
_, err := rand.Read(value)
92+
if err != nil {
93+
return BoiVar{}, err
94+
}
95+
96+
// We can't use .Set() here, because that tries to find the variable
97+
// in parent scopes.
98+
context.parentCtx.variables[key] = BoiVar{value}
99+
return BoiVar{value}, nil
100+
}
101+
78102
func BoiFuncCat(context *BoiContext, args []BoiVar) (BoiVar, error) {
79103
output := []byte{}
80104
for _, arg := range args {

main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ func NewBoiInterpreter(input []byte) *BoiInterpreter {
200200
boi.context.functions["dec"] = BoiFuncDec{boi}
201201
boi.RegisterGoFunction("<", BoiFuncLess)
202202

203+
// Grey area (memes, also practical)
204+
boi.RegisterGoFunction("declare", BoiFuncDeclare)
205+
203206
// Memes
204207
boi.RegisterGoFunction("IsEven", BoiFuncIsEven)
205208

@@ -309,6 +312,18 @@ func (boi *BoiInterpreter) getStatement() (*BoiStatement, error) {
309312

310313
return NewCallStatement("set", tokens), nil
311314

315+
case "one":
316+
fallthrough
317+
case "ONE":
318+
boi.pos += 4
319+
boi.noeof(boi.whitespace())
320+
tokens, err := boi.GetTokens()
321+
if err != nil {
322+
return nil, err
323+
}
324+
325+
return NewCallStatement("declare", tokens), nil
326+
312327
case "boi?":
313328
boi.pos += 4
314329
boi.noeof(boi.whitespace())
@@ -451,7 +466,8 @@ func (boi *BoiInterpreter) eatToken() (Token, error) {
451466
}
452467

453468
keyword := string(boi.rSyntaxToken.Find(boi.input[boi.pos:]))
454-
isBoi := keyword == "boi" || keyword == "]" || keyword == ";"
469+
isBoi := keyword == "boi" || keyword == "]" || keyword == ";" ||
470+
keyword == "BOI"
455471
if isBoi {
456472
boi.pos += IntyBoi(len(keyword))
457473
t := Token{

0 commit comments

Comments
 (0)