Git Product home page Git Product logo

lbc's People

Contributors

albeva avatar albeva-sainsburys avatar flatassembler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lbc's Issues

Add `typeof` keyword

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) ")" ;

Limitations

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

Implement type aliasing

Implement support for type aliases

Type IntPtr As Integer Ptr
Var i As Integer
Var ip As IntPtr = @i

Fix order of initialisation when using implicit main scope

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`

Add `REM` keyword

REM is traditional keyword in BASIC dialects to start a comment.

dim s as integer = 10 rem s is the player score

Refactor forward declaration logic

Implement consistent forward declaration in semantic analysis phase.

  • forward declare user defined types like udt, classes and type aliases
  • detect circular references
  • usable from module, udt and procedure level
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

Implementation

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.

Fix paren precedence

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

Add type alias support for typeof from declared functions.

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

Restrictions:

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

Implement type proxying

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

Fix RETURN handling

  • can appear at root level in implicit main file
  • can have statements following it

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.