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

Added optional type system - complain if proven wrong #150

Merged
merged 16 commits into from
May 12, 2016

Conversation

bvssvni
Copy link
Member

@bvssvni bvssvni commented May 11, 2016

See #80

This PR adds an optional type system using a similar, but different syntax than Rust.

  • bool (boolean)
  • f64 (number)
  • vec4 (4D vector)
  • []/[T] (array)
  • {} (object)
  • opt[T] (option)
  • res[T] (result)
  • any (any type, except void)
  • void (no value type)

Example

fn foo() -> opt[bool] {
    return some(true)
}

fn main() {
    if foo() {
        println("oh?")
    }
}
 --- ERROR --- 
In `source/test.rs`:
Type mismatch: Expected `bool`, found `opt[bool]`
6,8:     if foo() {
6,8:        ^

Any

An any type works with any other type except void.

Non-guarantee of argument type inside function body

Consider a simple example like this:

fn foo(a: bool) { ... }

It is not guaranteed that a has the type bool inside the function body. This is because the function can be called with an argument that is inferred to any at type checking.

Performance

The runtime performance should not be affected significantly, but the loading will take a little extra time.

Before:
test tests::bench_add         ... bench:  43,908,267 ns/iter (+/- 5,914,741)
test tests::bench_add_n       ... bench:  15,829,695 ns/iter (+/- 2,857,750)
test tests::bench_array       ... bench:  18,964,583 ns/iter (+/- 2,814,946)
test tests::bench_call        ... bench:  17,429,751 ns/iter (+/- 3,573,531)
test tests::bench_len         ... bench:   3,572,701 ns/iter (+/- 207,784)
test tests::bench_main        ... bench:   2,015,865 ns/iter (+/- 93,179) <---
test tests::bench_min         ... bench:  17,232,992 ns/iter (+/- 2,241,279)
test tests::bench_min_fn      ... bench:  81,652,195 ns/iter (+/- 5,507,277)
test tests::bench_n_body      ... bench:  76,455,628 ns/iter (+/- 4,989,369)
test tests::bench_object      ... bench:  26,533,172 ns/iter (+/- 3,704,094)
test tests::bench_primes      ... bench:  48,117,161 ns/iter (+/- 5,277,033)
test tests::bench_primes_trad ... bench:  47,062,226 ns/iter (+/- 7,232,953)
test tests::bench_sum         ... bench:   7,063,747 ns/iter (+/- 1,363,337)

After:
test tests::bench_add         ... bench:  44,384,373 ns/iter (+/- 5,417,288)
test tests::bench_add_n       ... bench:  17,259,780 ns/iter (+/- 2,925,107)
test tests::bench_array       ... bench:  19,701,937 ns/iter (+/- 3,805,578)
test tests::bench_call        ... bench:  18,241,928 ns/iter (+/- 3,989,516)
test tests::bench_len         ... bench:   3,745,375 ns/iter (+/- 240,261)
test tests::bench_main        ... bench:   2,196,033 ns/iter (+/- 114,887) <---
test tests::bench_min         ... bench:  17,259,160 ns/iter (+/- 2,536,126)
test tests::bench_min_fn      ... bench:  82,869,271 ns/iter (+/- 7,882,435)
test tests::bench_n_body      ... bench:  76,156,329 ns/iter (+/- 6,425,743)
test tests::bench_object      ... bench:  26,784,073 ns/iter (+/- 4,257,977)
test tests::bench_primes      ... bench:  48,005,609 ns/iter (+/- 5,273,527)
test tests::bench_primes_trad ... bench:  46,856,265 ns/iter (+/- 5,475,704)
test tests::bench_sum         ... bench:   7,120,835 ns/iter (+/- 654,078)

bvssvni added 16 commits May 11, 2016 14:33
- Added some types to syntax
- Added “types” module
- Added `types::Type`
- Added `Type::goes_with`
- Renamed “types” module to “typecheck”
- Added `Type::description`
- Added `Type::array`
- Added `Type::object`
- Added `Type::option`
- Added `Type::result`
- Fixed `Type::goes_with`
- Added syntax for optional space before and after arguments
- Added syntax for returning type
- Added types to snake example
- Added types to n_body benchmark
- Added type check test for opt
- Added type information to intrinsic functions
- Don’t panic on unknown kind in lifetime checker
- Added `PreludeFunction::tys`
Fixed some bugs in type checker
- Fixed syntax tests
- Added “typechk/return_3”
- Moved type checking from AST to lifetime checker graph
- Removed unused imports
- Fixed syntax tests
- Added “typechk/return_4”
- Change kind to `ReturnVoid`
- Set return type of mathematically declared functions to `Any`
- Added “typechk/add.rs”
- Added “typechk/mat_expr.rs”
- Require type from all children of Add nodes
- Added “item_extra” to meta syntax to help type checker
- Added “typechk/call.rs”
- Added “typechk/obj.rs”
@bvssvni
Copy link
Member Author

bvssvni commented May 12, 2016

This is just very basic type checking, but merging because it seem to work reasonably well and the more advanced stuff needs tweaking over longer time.

@bvssvni bvssvni merged commit 9516884 into PistonDevelopers:master May 12, 2016
@bvssvni bvssvni deleted the types branch May 12, 2016 10:35
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

Successfully merging this pull request may close these issues.

None yet

1 participant