Skip to content

Commit

Permalink
build(git): merged pull request #31 from plume-lang/feat/cf-statements
Browse files Browse the repository at this point in the history
Added support for while statements
  • Loading branch information
thomasvergne authored Aug 4, 2024
2 parents 9568fbe + 253cdbd commit d10914c
Show file tree
Hide file tree
Showing 83 changed files with 1,777 additions and 320 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_native.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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ CMakeFiles

node_modules

example/**/*.js
example/**/*.js

.idea
30 changes: 24 additions & 6 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Main where

import Control.Monad.Exception
import Control.Monad.Parser
import Data.Text.IO hiding (putStr)
import Data.Text.IO hiding (putStr, writeFile)
import Plume.Compiler.ClosureConversion.Conversion
import Plume.Compiler.Desugaring.Desugar
import Plume.Compiler.TypeErasure.EraseType
Expand Down Expand Up @@ -55,6 +55,13 @@ main = setEncoding $ do
MkOptions file_input ext_type file_output remove_prelude <- parseOptions
let output = fromMaybe file_input file_output

case ext_type of
"native" -> pure ()
"js" -> pure ()
_ -> do
ppFailure $ "Invalid backend, received: " <> fromString ext_type
exitFailure

env <- lookupEnv "PLUME_PATH"
mod' <- lookupEnv "PPM_PATH"

Expand All @@ -73,7 +80,9 @@ main = setEncoding $ do

paths <- fromEither [] <$> parse getPaths file content
let paths' = case env of
Just _ | not remove_prelude -> ("std:prelude", Nothing) : paths
Just _ | not remove_prelude -> do
let preludeName = if ext_type == "js" then "prelude-js.plm" else "prelude.plm"
("std:" <> preludeName, Nothing) : paths
_ -> paths

ppBuilding "Parsing file and dependencies..."
Expand All @@ -82,7 +91,7 @@ main = setEncoding $ do
void $ checkModule (env, mod') file

runConcreteToAbstract env dir paths' file `with` \ast -> do
let ast' = concatMap (removeUselessBlocks (False, False)) ast
let ast' = concatMap (removeUselessBlocks (False, True)) ast
ppBuilding "Typechecking..."
runSynthesize ast' `with` \tlir -> do
ppBuilding "Compiling and optimizing..."
Expand All @@ -97,9 +106,9 @@ main = setEncoding $ do

let outputPath = output -<.> "js"
writeFileText outputPath code
ppSuccess ("Javacsript code written to " <> fromString outputPath)
ppSuccess ("Javascript code written to " <> fromString outputPath)

_ -> do
"native" -> do
(bytecode, natives', constants) <- runLLIRAssembler desugared
let nativeFuns = getNativeFunctions natives'

Expand All @@ -112,11 +121,20 @@ main = setEncoding $ do
writeFileLBS newPath sbc
ppSuccess ("Bytecode written to " <> fromString newPath)

_ -> ppFailure "Invalid backend"

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

showBytecode :: [Instruction] -> Text
showBytecode bytecode =
unlines
[ show i <> ": " <> show instr
| (i, instr) <- zip [0 :: Int ..] bytecode
]
13 changes: 5 additions & 8 deletions example/basic/fibonacci.plm
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
require "helper"

fn fib_with_tco(n: int, a: int, b: int): int {
if n == 0 {
return a
} else {
return fib_with_tco(n - 1, b, a + b)
fn fib(n: int) =>
switch n {
case 0 => 0
case 1 => 1
case ? => fib(n - 1) + fib(n - 2)
}
}

fn fib(n: int) => fib_with_tco(n, 0, 1)

xs = range(0, 31).map(fn (n) => (n, fib(n)))

Expand Down
Loading

0 comments on commit d10914c

Please sign in to comment.