Coder Social home page Coder Social logo

xaxys / lxa Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 685 KB

A new programming language based on Lua vm.

License: MIT License

Go 64.20% C 34.65% C++ 1.15%
programming-language programming-languages programming language compiler interpreter lua virtual-machine vm lua-script lxa

lxa's Introduction

Lxa

A new programming language based on Lua vm.

I developed it for some learning purpose, but it haven't been used in any formal project.

WARNING! Please expect breaking changes and unstable APIs. Most of them are currently at an early, experimental stage.

Update

  • 2020/03/23 Released Lxa v0.1.0.
    • Use Azure/golua as vm.
  • 2020/03/25 Released Lxa v0.2.1.
    • Use Official lua 5.3.5 vm (written in c) as default vm.
    • Added inner go lua vm as a option (several stdlib unsupported yet).
    • Added 'Compile Only' option to output compiled lua bytecode (since v0.2.0).
    • Added linux support and x86 support (auto select static lib when compiling) (untested).
  • 2020/03/26 Released Lxa v0.2.4.
    • Added Debug option to display details in running.
    • Added 'Parse Only' option to watch bytecode instructions.
    • Optimized Logical Expression generation, now multiple and and or expressions are specially handled apart from Binary Expression.
    • Fixed bug of 0 length lua string in bytecode.
  • 2020/03/27 Updated to Lxa v0.2.5.
    • Optimized Logical Expression Handling.
    • Optimized Syntax Error Information.
  • 2020/04/03 Released Lxa v0.2.6.
    • Optimized Syntax Error Information.
    • Removed Assignment from ast.
    • Added BlockStat for sub code block in syntax.
    • Fixed bug caused by EmptyStat in ForNumStat, WhileStat and IfStat.
  • Draft
    • Fixed bug of LogicalExp in IfStat generation.

Syntax

Lxa has c-formed syntax with lua-based variable and data structure. Anyone who is familiar with C/C++/Java/Go can easily use it.

You can follow these examples or look EBNF below directly.

Lexical Conventions

Free-style Code, basically the same as Lua.

; is not necessary in the end of a sentence.

\n will be recognized as equal as ;.

&&,||,! can be also used as and, or,not

Removed ::, goto, repeat ,until ,do,elseif,end,then

Modified function to func, //(Integer Divide) to ~/, ~=(Not Equal) to !=, ^(Pow) to **, ~(Xor) to ^

Added +=, -=, *=, /=, ~/=, %=, **=, &=, |=, ^=, <<=, >>=, ++, --.

Added ? to judge if a number = 0 or a string length = 0.

(e.g.if a := 0; a? { print("is 0")}, or if a := ""; a? { print("a length is 0")})

Use { and } to recognize code block.

(WARNING: Because \n will be recognized as equal as ;, { is not allowed appeared in the next line)

Use // and /* */ to comment.

Assignment

a, b, c = 1, 2, 3
a, b = somefunction()

The same as Lua, global variable can be assigned directly.

Multiple value assignment is supported.

Local Variable Declaration

local a, b, c = 1, 2, 3
a, b, c := 1, 2, 3 //the same

The first statement is the same as Lua.

The second go-like statement is also supported which performs the same.

Statement

If

if a := 1; b := 2; testfunc(); a < b {
    a++
} else if c := a; c != d {
    print("c != d")
} else {
    print('else')
}

'If' statement can contain multiple assignment(or function call) before the expression, separated by ;.

The local variable declared by assignments can only be used inside 'if'.

While

while a := 0; a < 100 {
    a++
    if a > 50 {
        break
    }
}

The same as 'if' statement, a 'while' statement can contain multiple assignment(or function call) before the expression, separated by ;.

For

for i := 0; i < 100; i++ {
    print(i)
    continue
    print("unreachable")
}

The same as C/C++/Java/Go, a 'for' statement can only contain one assignment(or function call) before the expression and one after the expression, separated by ;.

ForIn

for k, v in ipairs(map0) {
    print("Key: "..k.." Value: "..v)
}

The same as Lua, a 'forin' statement invoke the function to iterate.

Tips

Lxa removed goto and repeat-until from syntax. But continue is added to syntax.

Function declaration

func add(a, b) {
    return a + b
}
local sub(a, b) {
    return a - b
}
mul = func(a, b) {
    return a * b
}
div := (a, b) => {
    return a / b
}

All Above function declaration are supported(lambda included).

Other

Other Lua feature are supported.

EBNF

chunk ::= block

block ::= {stat} [retstat]
retstat ::= return [explist] [';']
explist ::= exp {',' exp}
namelist ::= Name {',' Name}
varlist ::= var {',' var}
var ::=  Name | prefixexp '[' exp ']' | prefixexp '.' Name

assignment ::= assign | locvardecl | functioncall

assign ::= varlist ('+=' | '-=' | '*=' | '/=' | '~/=' | '%='
        | '&=' | '^=' | '|=' | '**=' | '<<=' | '>>=' | '=') explist

locvardecl ::= namelist ':=' explist | local namelist [':=' explist]

exp ::=  nil
    | false
    | true
    | Numeral
    | LiteralString
    | '...'
    | functiondef
    | functioncall
    | prefixexp
    | tableconstructor
    | exp binop exp
    | unop exp
    | varlist ['=' explist]
    | namelist ':=' explist

prefixexp ::= var
    | functioncall
    | '(' exp ')'
    | prefixexp ':' Name args
    | prefixexp args

functioncall ::=  prefixexp args | prefixexp ':' Name args

tableconstructor ::= '{' [fieldlist] '}'
fieldlist ::= field {fieldsep field} [fieldsep]
field ::= '[' exp ']' '=' exp | Name '=' exp | exp
fieldsep ::= ',' | ';'

functiondef ::= func funcbody | lambda
lambda ::= '(' [parlist] ')' '=>' '{' block '}'
funcbody ::= '(' [parlist] ')' '{' block '}'
parlist ::= namelist [',' '...'] | '...'
namelist ::= Name {',' Name}

stat ::= ';'
    | assignment ';'
    | break
    | continue
    | while {assignment ';'} exp '{' block '}'
    | if {assignment ';'} exp '{' block '}' {else if {assignment ';'} exp '{' block '}'} [else '{' block '}']
    | for assignment ';' exp ';' assignment '{' block '}'
    | for namelist in explist '{' block '}'
    | func funcname funcbody
    | local func Name funcbody

About

Contact me: E-mail: [email protected], QQ: 963796543, WebSite: http://www.oasis.run

lxa's People

Contributors

xaxys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.