Skip to content

Commit

Permalink
build(git): merged pull request #28 from plume-lang/feat/javascript-b…
Browse files Browse the repository at this point in the history
…ackend

Feat/javascript backend
  • Loading branch information
thomasvergne authored Jun 11, 2024
2 parents 1e7d636 + 3d2127a commit 90df162
Show file tree
Hide file tree
Showing 110 changed files with 11,670 additions and 3,589 deletions.
19 changes: 2 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,10 @@ jobs:
with:
submodules: true

- name: Install CLang on Windows and Ubuntu
uses: KyleMayes/install-llvm-action@v2
if: startsWith(matrix.os, 'windows') || startsWith(matrix.os, 'ubuntu')
with:
version: "17.0"

- uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: latest
actions-cache-folder: '.xmake-cache'
actions-cache-key: '${{matrix.os}}'

- name: Checking for dependencies
run: |
python3 --version
xmake --version --root
- name: Update xmake repository
run: xmake repo --update --root
node --version
- name: Set up GHC ${{ matrix.ghc-version }}
uses: haskell-actions/setup@v2
Expand Down Expand Up @@ -82,7 +67,7 @@ jobs:
key: ${{ steps.cache.outputs.cache-primary-key }}

- name: Build compiler and VM
run: python3 scripts/build_project.py --root
run: python3 scripts/build_project.py

- name: Create ZIP archive on UNIX
if: startsWith(matrix.os, 'ubuntu') || startsWith(matrix.os, 'macos')
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ compile_commands.json

*.plmc

CMakeFiles
CMakeFiles

node_modules

example/*.js
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

38 changes: 8 additions & 30 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ import Control.Monad.Exception
import Control.Monad.Parser
import Data.Either
import Data.Text.IO hiding (putStr)
import Plume.Compiler.LLIR.Assembler
import Plume.Compiler.Bytecode.Assembler
import Plume.Compiler.Bytecode.Serialize
import Plume.Compiler.Bytecode.Syntax (Instruction)
import Plume.Compiler.Bytecode.Label hiding (labelPool)
import Plume.Compiler.ClosureConversion.Conversion
import Plume.Compiler.Desugaring.Desugar
import Plume.Compiler.SSA
import Plume.Compiler.TypeErasure.EraseType
import Plume.Syntax.Abstract.Internal.Pretty ()
import Plume.Syntax.Parser.Modules.ParseImports
import Plume.Syntax.Translation.ConcreteToAbstract
import Plume.Compiler.Javascript.Translate
import Plume.TypeChecker.Checker
-- import Plume.Syntax.Memory
import Plume.Syntax.Blocks
Expand Down Expand Up @@ -77,35 +71,19 @@ main = setEncoding $ do
Nothing -> paths

ppBuilding "Parsing file and dependencies..."
writeIORef extensionType "js"
runConcreteToAbstract env dir paths' file `with` \ast -> do
let ast' = concatMap (removeUselessBlocks False) ast
ppBuilding "Typechecking..."
runSynthesize ast' `with` \tlir -> do
-- ppPrint tlir
ppBuilding "Compiling and optimizing..."
erased <- erase tlir
runClosureConversion erased `with` \closed -> do
desugared <- desugar closed
let ssa = runSSA desugared
-- mapM_ print ssa
(bytecode, natives', constants) <- runLLIRAssembler ssa
-- mapM_ print bytecode
let nativeFuns = getNativeFunctions natives'
(bytecode', labelPool) <- runUnlabelize bytecode

finalBytecode <- runBytecodeAssembler (labelPool, nativeFuns) bytecode'

sbc <- serialize (finalBytecode, natives', constants)
let new_path = file -<.> "bin"
writeFileLBS new_path sbc
ppSuccess ("Bytecode written to " <> fromString new_path)
Nothing -> ppFailure "No input file provided"

printBytecode :: [Instruction] -> IO ()
printBytecode bytecode =
mapM_
( \(i, instr) -> do
putStr (show i <> ": ")
print instr
)
(zip [0 :: Int ..] bytecode)
let js = runTranslateJS desugared
code = createMainJSApp js

writeFileText (replaceExtension file ".js") code
ppSuccess ("Bytecode written to " <> fromString (replaceExtension file ".js"))
Nothing -> ppFailure "No input file provided"
10 changes: 0 additions & 10 deletions example/cli.plm

This file was deleted.

39 changes: 33 additions & 6 deletions example/closure.plm
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
fn facto (n: int): int =>
switch n {
case 0 => 1
case ? => n * facto(n - 1)
}
interface<Env, A, Ret> Closure1<Env, A, Ret> {
fn apply1(env: Env, a: A): Ret
}

@println(facto(5))
interface<Env, A, B, Ret> Closure2<Env, A, B, Ret> {
fn apply2(env: Env, a: A, b: B): Ret
}

interface<Env, A, B, C, Ret> Closure3<Env, A, B, C, Ret> {
fn apply3(env: Env, a: A, b: B, c: C): Ret
}

interface<Env, A, B, C, D, Ret> Closure4<Env, A, B, C, D, Ret> {
fn apply4(env: Env, a: A, b: B, c: C, d: D): Ret
}

fn add(x: int) => fn(y: int) => x + y

// Would be converted to

type add_closure = int

extend Closure1<add_closure, int, int> {
fn apply1(env: add_closure, y: int): int => env + y
}

extend Closure1<unit, int, add_closure> {
fn apply1(_: unit, x: int): add_closure => x
}

x: add_closure = apply1(unit, 42)
y: int = apply1(x, 42)

println(y)
44 changes: 28 additions & 16 deletions example/extensions.plm
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
@println(3 ^ 3)
println(3 ^ 3)

println([1, 2, 3, 4, 5].show())
println([1, 2, 3, 4, 5])

@println("Hello, World!")
println("Hello, World!")

@println(Some(8))

extend str {
fn test(): str => "tesdt"
}
println(Some(8))

fn bruh(n: str) {
[n, n, n, n].map(println)
[n, n, n, n].map(fn(x) => println(x))

return n
}

@println(bruh("test"))
println(bruh("test"))

println("Hello" + "World")

println([1, 2, 3] == [4, 5, 6])
println([1, 2, 3] == [1, 2, 3])
println([1, 2, 3] != [4, 5, 6])
println([1, 2, 3] != [1, 2, 3])

@println("Hello" + "World")
println([1, 2, 3] + [4, 5, 6])

@println([1, 2, 3] == [4, 5, 6])
@println([1, 2, 3] == [1, 2, 3])
@println([1, 2, 3] != [4, 5, 6])
@println([1, 2, 3] != [1, 2, 3])
fn test(x: str) => fn(_: int): str {
return switch x {
case "a" => "b"
case ? {
if true {
return "c"
} else {
return "d"
}
}
}
}

@println([1, 2, 3] + [4, 5, 6])
x = test("a")(9)
println(x)
21 changes: 21 additions & 0 deletions example/factorial.plm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn facto (n: int): int =>
switch n {
case 0 => 1
case ? {
f = facto(n - 1)
return n * f
}
}


fn range(from: int, to: int): [int] {
if from == to {
return []
} else {
return [from] + range(from + 1, to)
}
}

xs = range(0, 21).map(fn(n) => (n, facto(n)))

xs.map(fn case (n, f) => println("$n! = $f"))
8 changes: 8 additions & 0 deletions example/fibonacci.plm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn fib(n: int): int =>
switch n {
case 0 => 0
case 1 => 1
case ? => fib(n - 1) + fib(n - 2)
}

println(fib(30))
Loading

0 comments on commit 90df162

Please sign in to comment.