albeva / lbc Goto Github PK
View Code? Open in Web Editor NEWSimple BASIC like toy compiler project
License: MIT License
Simple BASIC like toy compiler project
License: MIT License
Allow following: *subptr "hello"
which should be treated as (*subptr)("hello")
Support deducing type from a given expression. Similar to C++ decltype
Example:
rem legal:
dim a as typeof(AnotherType)
dim b as typeof(func) ptr
dim c as typeof(a + b)
dim d as typeof(3.1415)
dim e as typeof(not true)
dim f as typeof(null)
dim g as typeof(typeof(foo) ptr)
dim h = bar as typeof(moo)
rem illegal:
rem type foo as typeof(s.t)
rem declare sub foo(foo as typeof(x))
Grammer:
TypeOf = "typeof" "(" (Expr | TypeExpr) ")" ;
Because types and functions can be declared out of order and invoked without forward declarations - it is far too complex to allow type aliases or function parameters / return type to be deduced from typeof()
expression. So for now implement only for variable declarations.
The problem is with indirect type deductions. Because currently type declarations are parsed first and only then regular statements, it is far too complex to resolve this in a reasonably simple way:
dim s as MyFoo rem TypeProxy(unknown MyFoo)
s.hello = "Hello" rem type must be realized. But `f` is still unknown.
var f = Foo() rem to know type of `f` expression must solved
type MyFoo as typeof(f) rem TypeProxy(expression (f))
type Foo
hello as zstring
end type
DIM
is more traditional basic keyword for declaring variables.
Support UTF-8 source code input
Questions:
Allow declaring anonymous (unnamed) function / sub parameters. This is already allowed for proc type declarations.
Implement support for type aliases
Type IntPtr As Integer Ptr
Var i As Integer
Var ip As IntPtr = @i
Currently, variables are initialised in the lbc_global_var_init
global constructor, but some local statements can precede that, leading to the wrong order of executions. Investigate how Swift deals with this issue and maybe replicate it.
import cstd
srand time(null)
dim rnd = rand() ' will always return same value, as `rand()` gets called before `srand`
REM
is traditional keyword in BASIC dialects to start a comment.
dim s as integer = 10 rem s is the player score
Implement consistent forward declaration in semantic analysis phase.
type TFoo
var bar as TBar
end type
type TBar
var zoo as ABaz
end type
type ABaz as AZoo
type AZoo as TZoo
type TZoo
end type
Parse in multiple stages:
In the first AST pass we declare all symbols and their basic type kind (is it udt, type alias, procedure) - after pass all symbols should be known, but lack concrete types.
In the second AST pass we analyse the types and build up full type information.
in third and final stage we check for any unresolved types and for circular references.
Currently symbol tokens (()[]
) do not participate in operator precedence.
Meaning *fn()
is wrong. It should be treated as *(fn())
instead of (*fn)()
This should fix #5
Allow declaring a type alias to a function.
Base implementation #18
Make following code compile & run:
import cstd
dim f as FooFn = @foo rem TypeProxy(alias FooFn)
printf "%d\n", (*f)()
type FooFn as typeof(foo) ptr rem (function() as integer) ptr
function foo() as integer
return 42
end function
Must avoid circular aliases and not allow functions to use declared alias for params or return type.
Following is not legal:
type FooFn as typeof(foo) ptr rem TypeProxy(func foo)
type BarFn as typeof(bar) ptr rem error - circular type alias between BarFn and FooFn
declare function foo() as BarFn
declare function bar() as FooFn
Line 25 in 3687c4f
In order to allow late type definitions, implement a proxy type that can be updated and serves as a single source of truth for type information in the Ast
Logic is currently duplicated between TypePass
and FuncDeclarerPass
Add a check:
Line 83 in 3687c4f
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.