Skip to content

Add typechecking compiler pass#37

Merged
ecioppettini merged 19 commits into
masterfrom
enzo/bootstrap-typechecking-compiler-pass
Jul 8, 2025
Merged

Add typechecking compiler pass#37
ecioppettini merged 19 commits into
masterfrom
enzo/bootstrap-typechecking-compiler-pass

Conversation

@ecioppettini

Copy link
Copy Markdown
Contributor

Changes in this PR:

Parser

Add syntax for (optional) type annotations on variables.

let x : ty = ...

Add effect annotation syntax on coordination scripts

abi Abi {
  effect E1();
  effect E2();
}

script {
  fn foo(): u32 / { Abi } {
  }
}

I feel like using { } as delimiters here makes it a bit weird because it's too similar to the function body. But I also think it's the more proper syntax since that's the usual math notation for sets.

I don't know if it's worth changing the function syntax for it though. It's also the syntax that was on the slides I think.

Add syntax to annotate yield and resume in utxo

utxo Utxo {
  Yield u32
  Resume string
}

Added just to be able to typecheck the yield inside the main body, not necessarily because it's the best syntax. Also I couldn't find examples of these on the slides. We could put the annotation on the return type of the "function", but I don't see a good way of annotating the Resume that way.

Other

  1. There was a bug where loop { } was parsed as a record instead of a loop in the parser.
  2. Also changed raise to not take an expression, since effects are not really expressions.
  3. Added spans to expressions for errors. Still need to add some spans for other things though, some errors have a dummy span right now.

Typechecking

This adds a typechecking pass to the compiler after name resolution, with type inference for locals. We don't necessarily need to keep this restriction, since the algorithm should work anyway, but I'm not sure it's worth it.

There are still many holes though, but I tried to cover at least most of the things needed for coordination scripts. The examples that we have don't typecheck yet though. In particular with tokens I think there are some type annotations missing in the AST that need to be added before.

Open question

I'm not sure what's the proper syntax to resume a utxo with a type. Should it be utxo.resume(val)? We have a resume keyword in the syntax, but it was meant more for the effect handlers.

@ecioppettini
ecioppettini requested a review from SpaceManiac July 2, 2025 00:18
@ecioppettini ecioppettini self-assigned this Jul 2, 2025
@ecioppettini ecioppettini changed the title Enzo/bootstrap typechecking compiler pass bootstrap typechecking compiler pass Jul 2, 2025
@SpaceManiac

Copy link
Copy Markdown
Contributor

Should it be utxo.resume(val)?

This seems natural to me.

@SpaceManiac SpaceManiac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor things from first pass review, looks good overall


#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PrimitiveType {
Unit,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term, if we're looking to track the Wasm component model, we'll want I/U 8/16 and char. But this doesn't need to happen now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with having those types, but I'm not sure we necessarily need 1 to 1 parity to the wasm component model.

Comment thread starstream_compiler/src/codegen.rs Outdated
pub enum ComparableType {
// TODO: unify with codegen StaticType?
Primitive(PrimitiveType),
Intermediate,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe TokenIntermediate for clarity / to avoid confusion with codegen Intermediate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make that change. It's a bit confusing since the one in the ast it's also Intermediate. But eventually my idea was to unify both types, so we can fix it that way eventually.

To be honest I feel like Intermediate in general is not the best name to the type (in the language), I would maybe call it FreeToken , UnboundToken or something like that.

@SpaceManiac SpaceManiac changed the title bootstrap typechecking compiler pass Add typechecking compiler pass Jul 8, 2025
@ecioppettini
ecioppettini merged commit 87e12ae into master Jul 8, 2025
6 checks passed
@ecioppettini
ecioppettini deleted the enzo/bootstrap-typechecking-compiler-pass branch September 30, 2025 17:24
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.

2 participants