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 TypeScript support #4

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open

Conversation

Matts966
Copy link
Contributor

@Matts966 Matts966 commented Jan 24, 2020

Added TypeScript support, so if it is ok, it would be appreciated if this PR is merged.

TypeScript doesn't have templates, type classes, implicit parameters, by which deferred instantiation of types is possible. So this PR uses

  • Definition of type by
    • Deferred inference of conditional types
    • Type level recursion
    • This type is named Fluent in generated codes.
  • Unsafe implementation using any type
    • This implementation is named FluentImpl in generated codes, and the implementation itself is unsafe one, however, they are type-safe as in other languages for the sake of its typing called Fluent.

Setup

cd typelevelLR
cd examples/typescript
npm ci
(cd hello && typelevelLR --ts)
(cd syntax && typelevelLR --ts )
(cd while-lang && typelevelLR --ts)

Example usage of hello DSL by hello.ts

import * as helloDSL from "./helloDSL"

// default 
helloDSL.begin().hello().name("ok").end().accept()
// output: HelloWithName (NameString (ok))

// Formatted hello with name
helloDSL.begin()
    .hello()
    .name("OK")
    .end()
    .accept(new class visitor extends helloDSL.DefaultVisitor {
        visitHelloWithName(h : helloDSL.HelloWithName) {
            process.stdout.write("hello ")
            h.arg1.accept(this)
        }
        visitNameString(h : helloDSL.NameString) {
            console.log(h.arg1, "!")
        }
    }())
// output: hello OK !

// Formatted simple hello
helloDSL.begin()
    .hello()
    .end()
    .accept(new class visitor extends helloDSL.DefaultVisitor {
        visitSimpleHello() {
            console.log("Simple Hello!")
        }
    }())
// output: Simple Hello!

To run this

(cd hello && npx ts-node hello.ts)

Example usage of Syntax by app.ts

import * as Syntax from "./Syntax"

const syntax = Syntax.begin()
    .syntax("Syntax").startsWith("Start")
    .rule("DefineSyntax").derive("Syntax").to("To")
        .andThen("then").andThen("then")
    .rule("RulesCons").derive("derive").to("To").andThen("then")
    .rule("RulesNull").derive("derive").toEpsilon()
    .rule("RuleDerive").derive("derive").to("To").andThen("then")
    .rule("RuleTo").derive("derive").to("To").andThen("then")
    .rule("RuleToEpsilon").derive("derive").to("To")
    .rule("RuleTailTo").derive("derive").to("To").andThen("then")
    .rule("RuleTailEpsilon").derive("derive").toEpsilon()
    .end()

syntax.accept()

Example usage of WhileLanguage by app.ts

import * as While from "./WhileLanguage"

While.begin()
    .var("ok")
    .assign()
    .num(100)
    .end()
    .accept()

To run them

(cd (syntax|while-lang) && npx ts-node app.ts)

Type level recursion can cause Type instantiation is excessively deep and possibly infinite. error in some deep recursions, but the type-safety & code generation is successful for now as in this PR.

Also, the compilation time increase by method count is measured by randomchain-experiment.rb and it looks linear.

Any comments, advice, and requests are welcome. Thank you :)

@Matts966 Matts966 requested a review from ymzk January 24, 2020 20:19
@chibash
Copy link
Member

chibash commented Jan 31, 2020

Thank you for this great contribution!
We're sorry that reviewing takes time. Because it seems that we are he same locals, don't you have time to stop by and directly talk with us?

@Matts966
Copy link
Contributor Author

Matts966 commented Feb 2, 2020

I sent an email to the address in your profile. Thank you :)

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