Comments (3)
This is how I design the language: when you define a variable without a value, The magpie language will check if it is ended with a comma, it not, will report an error just like you got.
Is it possible to fix this?
Yes, if you really want to fix it, You can check parseLetStatement
function in parser.go
file:
1 func (p *Parser) parseLetStatement(inClass bool) *ast.LetStatement {
2 stmt := &ast.LetStatement{Token: p.curToken, InClass: inClass}
3 stmt.Doc = p.lineComment
4
5 if p.peekTokenIs(token.LPAREN) {
6 return p.parseLetStatement2(stmt)
7 }
8
9 //parse left hand side of the assignment
10 for {
11 p.nextToken()
12 if !p.curTokenIs(token.IDENT) && !p.curTokenIs(token.UNDERSCORE) {
13 msg := fmt.Sprintf("Syntax Error:%v- expected token to be identifier|underscore, got %s instead.", p.curToken.Pos, p.curToken.Type)
14 p.errors = append(p.errors, msg)
15 return stmt
16 }
17 name := &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}
18 stmt.Names = append(stmt.Names, name)
19
20 if !p.peekTokenIs(token.ASSIGN) && !p.curTokenIs(token.SEMICOLON) && !p.peekTokenIs(token.COMMA) {
21 stmt.SrcEndToken = p.curToken
22 return stmt
23 }
24
24 p.nextToken()
25 if p.curTokenIs(token.ASSIGN) || p.curTokenIs(token.SEMICOLON) {
26 break
27 }
28 if !p.curTokenIs(token.COMMA) {
29 msg := fmt.Sprintf("Syntax Error:%v- expected token to be comma, got %s instead.", p.curToken.Pos, p.curToken.Type)
30 p.errors = append(p.errors, msg)
31 return stmt
32 }
33 }
//.....
//.....
return stmt
}
You can add line 20~23
to the parseLetStatement
function, that will solve the problem.
Now you can do below:
let a b=10
printf("a=%v\n", a) //will print 'a=nil'
printf("b=%d\n", b) //will print `b=10`
Through above code woks, but it's ugly. For readability, you can code it like below:
let a
b=10
printf("a=%v\n", a) //will print 'a=nil'
printf("b=%d\n", b) //will print `b=10`
But... I also think a semicolon after variable a
should be better. so I will not modify the source.
from magpie.
@haifenghuang Thanks, now I understood how it works.
from magpie.
I've changed the parsing for let
statement, so it now can accept an optional ';' for example:
let x,y
let m,n;
Both are valid let
statement now.
from magpie.
Related Issues (17)
- hash vivification not working HOT 15
- Question: have you planned to make Magpie compilable or bytecode? HOT 2
- Question: When do you want to implement interface, public, private, protected keywords? HOT 1
- [REPL Bug] stdin does not scans full string. HOT 1
- [REPL Bug] include errors ignoring. HOT 1
- How to check the typeof obj ? HOT 2
- Could Break to Label? HOT 1
- Is there go-routine feature? HOT 1
- howto get properties from json object HOT 2
- Hello HOT 1
- tcp的read方法阻塞 HOT 1
- idea: add namespace management HOT 6
- Please add multiline code blocks to the REPL. HOT 5
- Errors in tests. HOT 4
- I found a way to add support for highlight on Windows HOT 2
- Compilation succeed for Windows but failed for other platforms. HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magpie.