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

Add typeof keyword #18

Closed
albeva opened this issue Mar 1, 2022 · 0 comments · Fixed by #22
Closed

Add typeof keyword #18

albeva opened this issue Mar 1, 2022 · 0 comments · Fixed by #22

Comments

@albeva
Copy link
Owner

albeva commented Mar 1, 2022

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
albeva added a commit that referenced this issue Mar 2, 2022
albeva added a commit that referenced this issue Mar 2, 2022
This allows instantiating parser with other token input sources than a lexer.
albeva added a commit that referenced this issue Mar 3, 2022
albeva added a commit that referenced this issue Mar 3, 2022
albeva added a commit that referenced this issue Mar 4, 2022
…y tries to parse a type and if that fails than try parse an expression.
albeva added a commit that referenced this issue Mar 4, 2022
albeva added a commit that referenced this issue Mar 4, 2022
albeva added a commit that referenced this issue Mar 5, 2022
albeva added a commit that referenced this issue Mar 5, 2022
albeva added a commit that referenced this issue Mar 5, 2022
@albeva albeva closed this as completed in #22 Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant