Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request ljs2lua #6

Open
bferguson3 opened this issue Jun 8, 2020 · 4 comments
Open

Request ljs2lua #6

bferguson3 opened this issue Jun 8, 2020 · 4 comments

Comments

@bferguson3
Copy link

I'd like to be able to code using ljs and then run it in an interpreted environment that uses standard lua 5.1 code (eg love2d/lovr).

Can you make a reverse converter?

@mingodad
Copy link
Owner

mingodad commented Jun 9, 2020

Probably yes but I do not need it, could you do it too and share it ?

@Shadowblitz16
Copy link

I would like this too for love2d

@Artoria2e5
Copy link

Artoria2e5 commented May 19, 2021

It would really help to have a grammar instead of following the C parser. I know it's a simple recursive-descent, but code is always more complicated to the eye than what's in lua-parser.ly.

Having more parsers also opens up the opportunity to get code formatters. A port of prettier-lua to ljs would... fix some complaints about how many people prefers to have a space after flow-control keywords and no space before the : in typing.

@mingodad
Copy link
Owner

mingodad commented May 19, 2021

Thank you for your interest and feedback !
Here you have an EBNF grammar compatible with https://www.bottlecaps.de/rr/ui for railroad diagram generation (I need to add it to the repository eventually). Copy the grammar bellow and paste on the Edit Grammar tab then switch to the View Diagram tab on https://www.bottlecaps.de/rr/ui (any feedback is welcome).

//"--" lf cr  '+' lf  '+' tab
Ljs ::= statlist
statlist ::=  ( statement  )*
statement ::= ';'  | ifstat  | whilestat  | block  | forstat  | dowhilestat  | funcstat  |  ( "local"  | "var"  | "let"  | "auto"  )  ( "function" localfunc  | localstat  )  | labelstat  | "return" retstat  | gotostat  | exprstat
ifstat ::= "if" test_then_block  ( "else" statement  )?
test_then_block ::= '(' expr ')' statement
block ::= '{' statlist '}'
whilestat ::= "while" '(' cond ')' statement
forstat ::= "for" '(' TK_NAME  ( fornum  | forlist  )
fornum ::= '=' expr ',' expr  ( ',' expr  )? forbody
forlist ::=  ( ',' TK_NAME  )* TK_IN explist forbody
forbody ::= ')' statement
dowhilestat ::= "do" '{' statlist '}' "while" '(' cond ')'
cond ::= expr
funcstat ::= "function" funcname body
funcname ::= singlevar  ( '.' fieldsel  )*  ( "::" fieldsel  )?
singlevar ::= str_checkname
body ::= '('  ( parlist  )? ')'  ( ':' TK_NAME  )? '{' statlist '}'
parlist ::= TK_DOTS  ( ':' TK_NAME  )?  | TK_NAME  ( ':' TK_NAME  )?  ( ','  ( TK_NAME  | TK_DOTS  )  ( ':' TK_NAME  )?  )*
localfunc ::= TK_NAME body
localstat ::= name_attribute  ( ',' name_attribute  )*  ( '=' explist  )?
name_attribute ::= TK_NAME  ( getlocalattribute  )?  ( ':' TK_NAME  )?
getlocalattribute ::= '<'  ( "const"  | "close"  ) '>'
labelstat ::= TK_NAME ':'
retstat ::=  ( explist  )?  ( ';'  )?
gotostat ::= "goto" str_checkname  | "break"  | "continue"
inc_dec_op ::=  ( "++"  | "--"  )
exprstat ::= suffixedexp  ( assign_compound  | assignment  | ';'  )?
assignment ::= ','  | '='
assign_compound ::= TK_CADD  | TK_CSUB  | TK_CMUL  | TK_CDIV  | TK_CMOD
suffixedexp ::= primaryexp  ( '.' fieldsel  | '[' yindex  | "->" checkname funcargs  | funcargs  | inc_dec_op  )*
primaryexp ::= '(' expr ')'  | singlevar  | inc_dec_op
fieldsel ::= checkname
checkname ::= codestring
codestring ::= str_checkname
str_checkname ::= TK_NAME
yindex ::= expr ']'
funcargs ::= '('  ( explist  )? ')'
tconstructor ::=  ( field  (  ( ','  | ';'  ) field  )*  )? '}'
field ::= TK_NAME recfield  | '[' yindex recfield  | listfield
listfield ::= expr
recfield ::= '=' expr
explist ::= expr  ( ',' expr  )*
expr ::= subexpr  ( '?' expr ':' expr  )?
subexpr ::=  (  ( getunopr subexpr  )  | simpleexp  )  ( getbinopr subexpr  )*
getunopr ::= "not"  | '-'  | '~'  | '#'
getbinopr ::= '+'  | '-'  | '*'  | '%'  | '^'  | '/'  | "idiv"  | '&'  | '|'  | '!'  | "<<"  | ">>"  | ".."  | "!="  | "=="  | '<'  | "<="  | '>'  | ">="  | "&&"  | "||"
simpleexp ::= TK_FLT  | TK_INT  | TK_STRING  | TK_NIL  | TK_TRUE  | TK_FALSE  | TK_DOTS  | '{' tconstructor  | TK_FUNCTION body  | suffixedexp

letter ::= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
oct ::= '0'  .. '7'
digit ::= '0'  .. '9'
nzdigit ::= '1'  .. '9'
cr ::= '\r'
lf ::= '\n'
tab ::= '\t'
stringCh ::= ANY  '-' '"'  '-' '\'  '-' cr  '-' lf
charCh ::= ANY  '-' "'"  '-' '\'  '-' cr  '-' lf
longStringCh ::= ANY  '-' "]]"
printable ::= '\u0020'  .. '\u007e'
hex ::= digit  '+' 'a'  .. 'f'  '+' 'A'  .. 'F'
TK_NAME  ::= letter  ( letter  | digit  )*
TK_FLT  ::=  ( '.' digit  ( digit  )*  (  ( 'e'  | 'E'  )  ( '+'  | '-'  )? digit  ( digit  )*  )?  | digit  ( digit  )* '.'  ( digit  )*  (  ( 'e'  | 'E'  )  ( '+'  | '-'  )? digit  ( digit  )*  )?  | digit  ( digit  )*  ( 'e'  | 'E'  )  ( '+'  | '-'  )? digit  ( digit  )*  )
TK_INT  ::=  ( digit  ( digit  )*  |  ( "0x"  | "0X"  ) hex  ( hex  )*  )
TK_STRING  ::= '"'  ( stringCh  | '\'  ( printable  |  ( '\r'  )? '\n'  )  )* '"'  | "'"  ( charCh  | '\'  ( printable  |  ( '\r'  )? '\n'  )  )* "'"  | "[["  ( longStringCh  )* "]]"
badString  ::= '"'  ( stringCh  | '\' printable  )*  ( cr  | lf  )  | "'"  ( charCh  | '\' printable  )*  ( cr  | lf  )
TK_AUTO  ::= "auto"
TK_BREAK  ::= "break"
TK_CONTINUE  ::= "continue"
TK_DO  ::= "do"
TK_ELSE  ::= "else"
TK_FALSE  ::= "false"
TK___FILE__  ::= "__FILE__"
TK_FOR  ::= "for"
TK_FUNCTION  ::= "function"
TK_GOTO  ::= "goto"
TK_IDIV  ::= "idiv"
TK_IF  ::= "if"
TK_IN  ::= "in"
TK_LET  ::= "let"
TK___LINE__  ::= "__LINE__"
TK_LOCAL  ::= "local"
TK_NIL  ::= "null"
TK_RETURN  ::= "return"
TK_TRUE  ::= "true"
TK_VAR  ::= "var"
TK_WHILE  ::= "while"
TK_AND  ::= "&&"
TK_NOT  ::= '!'
TK_OR  ::= "||"
TK_POW  ::= "**"
TK_CONCAT  ::= ".."
TK_DOTS  ::= "..."
TK_EQ  ::= '='
TK_GE  ::= ">="
TK_LE  ::= '<'
TK_NE  ::= "!="
TK_CADD  ::= "+="
TK_CSUB  ::= "-="
TK_CMUL  ::= "*="
TK_CDIV  ::= "/="
TK_CMOD  ::= "%="
TK_CCONCAT  ::= "..="
TK_PLUSPLUS  ::= "++"
TK_MINUSMINUS  ::= "--"
TK_SHL  ::= "<<"
TK_SHR  ::= ">>"
TK_ARROW  ::= "->"
TK_DBCOLON  ::= "::"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants