Coder Social home page Coder Social logo

Question: let keyword about magpie HOT 3 CLOSED

haifenghuang avatar haifenghuang commented on July 29, 2024
Question: let keyword

from magpie.

Comments (3)

haifenghuang avatar haifenghuang commented on July 29, 2024

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.

 avatar commented on July 29, 2024

@haifenghuang Thanks, now I understood how it works.

from magpie.

haifenghuang avatar haifenghuang commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.