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 support for built-in and standard types #44

Merged
merged 1 commit into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![release](https://img.shields.io/github/v/release/melvic-ybanez/chi?include_prereleases) ![issues](https://img.shields.io/github/issues/melvic-ybanez/chi) ![loc](https://img.shields.io/tokei/lines/github/melvic-ybanez/chi)
[![License](https://img.shields.io/badge/license-MIT-green)](./LICENSE)

A code generator for fully parametric functions.
A code generator for fully parametric functions and functions between selected set of built-in and standard types.
Chi stands
for [Curry-Howard Isomorphism](https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence).

Expand Down Expand Up @@ -150,3 +150,23 @@ Generated code:
};
}
```

# Built-in types
Chi also recognize built-in types:
```scala
chi> String idString(String s)
Detected language: Java
Generated code:
String idString(String s) {
return s;
}

chi> def foo(f: String => Int, g: Float => Int): Either[String, Float] => Int
Detected language: Scala
Generated code:
def foo(f: (String => Int), g: (Float => Int)): (Either[String, Float] => Int) =
e => e match {
case Left(s) => f(s)
case Right(h) => g(h)
}
```
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.melvic.chi.eval

import com.melvic.chi.ast.Proposition.{Identifier, PUnit}
import com.melvic.chi.ast.Proposition.{Atom, Identifier, PUnit}
import com.melvic.chi.ast.{Definition, Proposition, Signature}
import com.melvic.chi.env.Env
import com.melvic.chi.out.Fault.UnknownPropositions
import com.melvic.chi.out.Result
import com.melvic.chi.out.Result.Result
import com.melvic.chi.parsers.{JavaParser, Language, ScalaParser}

object CodeGen {
object Generate {
def fromSignature(signature: Signature, language: Language): Result[Definition] = {
val Signature(name, typeParams, params, proposition) = signature

val unknownTypes = Proposition.filter(proposition) {
case PUnit => false
case atom =>
!typeParams.map(Identifier).contains(atom)
case atom @ Atom(value) =>
!typeParams.map(Identifier).contains(atom) && !Language.builtInTypes(language).contains(value)
}

if (unknownTypes.nonEmpty) Result.fail(UnknownPropositions(unknownTypes))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/melvic/chi/eval/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package object eval {
type Evaluate = String => String

def generateAndShow: Evaluate = code =>
Result.show(CodeGen.fromSignatureString(code))
Result.show(Generate.fromSignatureString(code))
}
2 changes: 1 addition & 1 deletion src/main/scala/com/melvic/chi/package.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.melvic

import com.melvic.chi.eval.{CodeGen, Prover}
import com.melvic.chi.eval.{Generate, Prover}
import com.melvic.chi.out.Result

package object chi {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/com/melvic/chi/parsers/JavaParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ object JavaParser extends BaseParser {
lazy val proposition: PackratParser[Proposition] = function | biFunction | identifier

val functionCode: Parser[Signature] =
typeParams ~ proposition ~ nameParser ~ opt(paramList) ^^ {
opt(typeParams) ~ proposition ~ nameParser ~ opt(paramList) ^^ {
case typeParams ~ returnType ~ name ~ params =>
Signature(name, typeParams.map(_.value), params.getOrElse(Nil), returnType)
Signature(name, typeParams.getOrElse(Nil).map(_.value), params.getOrElse(Nil), returnType)
}
}
11 changes: 11 additions & 0 deletions src/main/scala/com/melvic/chi/parsers/Language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@ sealed trait Language
object Language {
case object Java extends Language
case object Scala extends Language

def builtInTypes(language: Language): List[String] = {
val builtinTypeString = language match {
case Scala => "Char,Byte,Short,Int,Long,Float,Double,String"
case Java =>
val primitive = "byte,short,int,long,float,double,boolean,char"
val boxed = "Byte,Boolean,Char,Float,Int,Long,Short,Double,String"
primitive + boxed
}
builtinTypeString.split(",").toList
}
}
10 changes: 10 additions & 0 deletions src/test/scala/com/melvic/chi/tests/JavaFunctionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ class JavaFunctionsSpec extends AnyFlatSpec with should.Matchers {
|}""".stripMargin
)
}

"Built-in types" should "be included in the type search" in {
generateAndShow("String idString(String s)") should be(
"""Detected language: Java
|Generated code:
|String idString(String s) {
| return s;
|}""".stripMargin
)
}
}
12 changes: 12 additions & 0 deletions src/test/scala/com/melvic/chi/tests/ScalaDefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,16 @@ class ScalaDefSpec extends AnyFlatSpec with should.Matchers {
| }""".stripMargin
)
}

"Built-in types" should "be included in the type search" in {
generateAndShow("def foo(f: String => Int, g: Float => Int): Either[String, Float] => Int") should be(
"""Detected language: Scala
|Generated code:
|def foo(f: (String => Int), g: (Float => Int)): (Either[String, Float] => Int) =
| e => e match {
| case Left(s) => f(s)
| case Right(h) => g(h)
| }""".stripMargin
)
}
}