pandeykartikey / goto Goto Github PK
View Code? Open in Web Editor NEWGoto is an interpreted programming language written in go.
License: MIT License
Goto is an interpreted programming language written in go.
License: MIT License
I had this issue with my overhauled/enhanced version of monkey, so it was the first thing I looked for:
Sample program:
cat len.goto
print( len( "狐犬" ) );
Execution:
goto len.goto
6
null
You want to change your primitive to use something like this:
return &object.Integer{Value: int64(utf8.RuneCountInString(arg.Value))}
Rather than:
return &object.Integer{Value: int64(len(arg.Value))}
The following program hangs forever:
$ cat in.go
print( "steve );
The following quick patch is one potential solution:
diff --git a/lexer/lexer.go b/lexer/lexer.go
index dafc23a..48c7654 100644
--- a/lexer/lexer.go
+++ b/lexer/lexer.go
@@ -47,7 +47,7 @@ func (l *Lexer) skipComments() {
func (l *Lexer) readSequence(f func(byte) bool) string {
position := l.position
- for f(l.ch) {
+ for l.ch != 0 && f(l.ch) {
l.readChar()
}
Ideally you'd return token.ILLEGAL
if the function didn't match and you hit EOF, so perhaps something better needs to be done.
The following program panics:
$ cat div.to
print( 1 / 0 );
$ goto div.to
panic: runtime error: integer divide by zero
goroutine 1 [running]:
github.com/pandeykartikey/goto/eval.evalInfixIntegerExpression(0x5f95ef, 0x1, 0xc0000b4048, 0xc0000b4040, 0x0, 0x410d1b)
/home/skx/go/src/github.com/pandeykartikey/goto/eval/eval.go:113 +0x6fc
github.com/pandeykartikey/goto/eval.evalInfixExpression(0x5f95ef, 0x1, 0x54df20, 0xc0000b4048, 0x54df20, 0xc0000b4040, 0x7f0ab170e100, 0x20300000000000)
..
You probabl want to catch that in evalInfixIntegerExpression
and return an error, something like:
frodo ~/go/src/github.com/pandeykartikey/goto $ git diff eval/eval.go
diff --git a/eval/eval.go b/eval/eval.go
index c2e3e43..9a05b11 100644
--- a/eval/eval.go
+++ b/eval/eval.go
@@ -110,6 +110,9 @@ func evalInfixIntegerExpression(op string, left *object.Integer, right *object.I
case "*":
return &object.Integer{Value: leftVal * rightVal}
case "/":
+ if rightVal == 0 {
+ return errorMessageToObject("division by zero")
+ }
return &object.Integer{Value: leftVal / rightVal}
case "%":
return &object.Integer{Value: leftVal % rightVal}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.