Skip to content

Commit

Permalink
Merge pull request #64 from joaovitorsilvestre/arrumando_a_casa
Browse files Browse the repository at this point in the history
Simplifying a lot the bootstrap and testing, using docker
  • Loading branch information
joaovitorsilvestre authored Oct 31, 2022
2 parents 17830ea + cd8c78f commit 06e8ad1
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 100 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/_compiled/
/bin/
/Makefile
**/erl_crash.dump
72 changes: 24 additions & 48 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
SHELL := /bin/bash
ev=latest

.SILENT: compile, bootstrap

TEMP_FILES = /tmp
THIS_GIT_VERSION := $(shell git tag | tail -1)

INSTALATION_PATH := /opt/fython

# makefile location
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
# fython src folder
SRC_DIR:="$(ROOT_DIR)/src"

install-from-source:
$(MAKE) compile-source DESTINE_PATH=$(INSTALATION_PATH)

shell:
erl -pa $(INSTALATION_PATH) -s 'Fython.Shell' start -noshell

compile-source:
echo "Bootstraping with version: " $(THIS_GIT_VERSION)
echo "Destine path: " $(DESTINE_PATH)

$(eval OUTPUT_ZIP_PATH := $(TEMP_FILES)/fython_$(THIS_GIT_VERSION)_compiled.tar.gz)
wget https://github.com/joaovitorsilvestre/fython/releases/download/$(THIS_GIT_VERSION)/_compiled.tar.gz -O $(OUTPUT_ZIP_PATH)

$(eval COMPILED_OUTPUT_PATH := $(TEMP_FILES)/fython_$(THIS_GIT_VERSION)_compiled_local)
sudo mkdir -p $(COMPILED_OUTPUT_PATH) 2>/dev/null
sudo tar -xf $(OUTPUT_ZIP_PATH) -C $(COMPILED_OUTPUT_PATH)

$(MAKE) bootstrap PRE_COMPILER=$(COMPILED_OUTPUT_PATH) DESTINE_PATH=$(DESTINE_PATH)

bootstrap:
# e.g: make bootstrap PRE_COMPILER=/home/joao/fython/src/_compiled DESTINE_PATH=/home/joao/fython/src/_bootstrap

$(eval ELIXIR_PATH = "/usr/lib/elixir/lib/elixir/ebin")
$(eval ALL_FILES_PATH := $(shell find $(SRC_DIR) -name '*.fy'))

echo "Bootstraping"
echo "pre compiler: "$(PRE_COMPILER)
echo "destine folder: "$(DESTINE_PATH)

sudo rm -rf $(DESTINE_PATH) || 0
sudo mkdir $(DESTINE_PATH)

# copy elixir modules
sudo cp -r $(ELIXIR_PATH)/* $(DESTINE_PATH)

# compile all the modules idependitly
for FILE_PATH in $(ALL_FILES_PATH); do ./functions.sh exec_in_erl ${SRC_DIR} $${FILE_PATH} ${DESTINE_PATH} ${PRE_COMPILER}; done

compress-to-release:
cd $(FOLDER_PATH)/ && tar -zcvf $(ROOT_DIR)/_compiled.tar.gz * && cd -
bootstrap-with-docker:
@DOCKER_TAG="fython:bootstrap"
docker build -f devtools/Dockerfile -t $$DOCKER_TAG --target base .

.ONESHELL:
compile-project:
# USAGE:
# > make compile-project path=/home/joao/fython/example
@DOCKER_TAG="fython:compiler"
docker build -f devtools/Dockerfile -t $$DOCKER_TAG --target compiler .
docker run --env PROJET_FOLDER=/project -v $(path):/project $$DOCKER_TAG

.ONESHELL:
run-tests:
@DOCKER_TAG="fython:tests"
docker build -f devtools/Dockerfile -t $$DOCKER_TAG --target tests .
docker run $$DOCKER_TAG


.ONESHELL:
shell-current-src:
@DOCKER_TAG="fython:shell"
docker build -f devtools/Dockerfile -t $$DOCKER_TAG --target shell .
docker run -it $$DOCKER_TAG
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ b = 'RPAREN'
case b:
a -> False # this is not working
```
- [ ] Some times the error arrow is showing in wrong place. Eg:
- [ ] Sometimes the error arrow is showing in wrong place. Eg:
```
def add(a)
a + b
Expand Down Expand Up @@ -181,6 +181,15 @@ a = lambda:
lambda : ""
a()()
```
- [ ] Support of 'pass' keyword
- [ ] Create function to use for operator +
- this will allow us to create multiple functions depending on params
- if its two strings, concatenate, if is lists, merge them, etc
- today elixir has diferent functions, to merge lists is ++ and strings <>
- [ ] Improve way to access tuples elements, today we need to use Elixir.Kernel.elem
- Would be great if our Enum module worked with tuples too!
- [ ] Create simple lib of testing that collect functions in folder and run them
- [ ] Print function
- [ ] Support to struct
- [ ] PosParser -> Add logic to check imports, undefined vars, etc.
- [x] PosParser -> support for the pin variable in pattern matching: `e = "a""; {^e: 1} = {"a": 1}`
Expand Down
51 changes: 51 additions & 0 deletions devtools/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FROM elixir:1.14.1 as base

# Arguments you can modify
ARG DESTINE_PATH="/compiled"
ARG VERSION_TO_USE_AS_BOOTSTRAPER="v0.5.2"

ENV DESTINE_PATH=$DESTINE_PATH
ENV PATH_FYTHON_TO_USE_AS_BOOTSTRAPER="/fython_$VERSION_TO_USE_AS_BOOTSTRAPER"
ENV ELIXIR_BEAMS_PATH="/usr/local/lib/elixir/lib/elixir/ebin/"
ENV IEX_BEAMS_PATH="/usr/local/lib/elixir/lib/iex/ebin/"

# geting older version to use for bootstrap
ENV OUTPUT_ZIP_PATH="/fython_$VERSION_TO_USE_AS_BOOTSTRAPER_compiled.tar.gz"
RUN wget https://github.com/joaovitorsilvestre/fython/releases/download/$VERSION_TO_USE_AS_BOOTSTRAPER/_compiled.tar.gz -O $OUTPUT_ZIP_PATH \
&& mkdir -p $PATH_FYTHON_TO_USE_AS_BOOTSTRAPER \
&& tar -xf $OUTPUT_ZIP_PATH -C $PATH_FYTHON_TO_USE_AS_BOOTSTRAPER

COPY src src
COPY /devtools/bootstrap.sh /bootstrap.sh
COPY /devtools/fython /fython
RUN chmod a+x /bootstrap.sh
RUN chmod a+x /fython

# Execute bootstrap
RUN echo "Bootstraping using Fython $VERSION_TO_USE_AS_BOOTSTRAPER" \
&& ./bootstrap.sh compile /src $DESTINE_PATH $PATH_FYTHON_TO_USE_AS_BOOTSTRAPER $ELIXIR_BEAMS_PATH

# Remove base used for bootstrap (to ensure that we are not using it anymore)
RUN rm -rf $PATH_FYTHON_TO_USE_AS_BOOTSTRAPER

# Check if it can recompile itself (its a good way to test while we dont have proper testing)
#RUN ./bootstrap.sh compile /src /test_compiled1 $DESTINE_PATH $ELIXIR_BEAMS_PATH
#RUN ./bootstrap.sh compile /src /test_compiled2 /test_compiled1

FROM base as elixir_shell
# TODO
# CMD erl -pa $IEX_BEAMS_PATH -noshell -user Elixir.IEx.CLI +iex

FROM base as shell
CMD /fython exec "Shell.start()" $DESTINE_PATH

FROM base as compiler
ENV PROJET_FOLDER="/project"
CMD echo "Compiling project: $PROJET_FOLDER" \
&& /fython exec "Core.Code.compile_project('$PROJET_FOLDER')" $DESTINE_PATH

FROM base as tests
COPY tests tests

CMD /fython exec "Core.Code.compile_project('/tests')" $DESTINE_PATH \
&& /fython exec "Math_tests.run_tests()" $DESTINE_PATH /tests/_compiled
26 changes: 26 additions & 0 deletions devtools/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -e # stop if any error

compile () {
# We use the _COMP sufix to avoid conflict with external variables
# (variabls of the shell that is running this script)
SRC_DIR_COMP=$1
DESTINE_PATH_COMP=$2
PATH_FYTHON_TO_USE_AS_BOOTSTRAPER_COMP=$3
ELIXIR_BEAMS_PATH_COMP=$4

ALL_FILES_PATH_COMP=$(find $SRC_DIR_COMP -name '*.fy')

echo "Destine folder: $DESTINE_PATH_COMP"
rm -rf $DESTINE_PATH_COMP
mkdir $DESTINE_PATH_COMP

cd $ELIXIR_BEAMS_PATH_COMP && cp -r . "$DESTINE_PATH_COMP" && cd /

for FILE_PATH in $ALL_FILES_PATH_COMP; do
ERL_COMMAND_CALL="application:start(compiler), application:start(elixir), 'Elixir.Code':compiler_options(#{ignore_module_conflict => true}), 'Fython.Core.Code':compile_project_file(<<"'"'$SRC_DIR_COMP'"'">>, <<"'"'${FILE_PATH}'"'">>, "'"'$DESTINE_PATH_COMP'"'"), init:stop().";
erl -pa $PATH_FYTHON_TO_USE_AS_BOOTSTRAPER_COMP -noshell -eval "$ERL_COMMAND_CALL";
done
}

$*
15 changes: 15 additions & 0 deletions devtools/fython
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e # stop if any error

exec () {
FYTHON_CODE=$1
ERLANG_PATHS=${@:2}

ERLANG_PATHS_FORMATED=""
for ERL_PATH in $ERLANG_PATHS; do
ERLANG_PATHS_FORMATED=$ERLANG_PATHS_FORMATED"-pa $ERL_PATH "
done
erl $ERLANG_PATHS_FORMATED -noshell -eval "'Fython.Core':eval_string(<<"'"'"<stdin>"'"'">>, <<"'"'"$FYTHON_CODE"'"'">>)." -run init stop
}

$*
16 changes: 0 additions & 16 deletions functions.sh

This file was deleted.

31 changes: 0 additions & 31 deletions src/bootstrap.sh

This file was deleted.

4 changes: 1 addition & 3 deletions src/core/code.fy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def compile_project(project_path, destine):
|> Elixir.Enum.join('/')
|> Elixir.Path.wildcard()
|> Elixir.Enum.map(lambda file_full_path:
compile_project_file(project_path, file_full_path, destine)
compile_project_file(project_path, file_full_path, compiled_folder)
)


Expand All @@ -40,8 +40,6 @@ def copy_elixir_beams(compiled_folder):
def compile_project_file(project_root, file_full_path, destine_compiled):
module_name = get_module_name(project_root, file_full_path)

destine_compiled = Elixir.Enum.join([destine_compiled])

# Ensure compiled folder is created
Elixir.File.mkdir_p!(destine_compiled)

Expand Down
9 changes: 8 additions & 1 deletion src/core/lexer/__init__.fy
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def loop_while(st, func):
case valid:
True -> Elixir.Map.put(st, "result", Elixir.Enum.join([result, cc])) |> loop_while(func)
False -> st
_ -> set_error(st, valid)


def loop_until_sequence(state, expected_seq):
Expand Down Expand Up @@ -362,9 +363,15 @@ def make_number(state):
None -> False
_ -> Elixir.String.contains?(Elixir.Enum.join([Core.Lexer.Consts.digists(), '._']), cc)

valid_num_char and cc != "." and nc != "."
case (valid_num_char, cc, nc):
(False, _, _) -> False
(True, ".", ".") -> False # Range
(True, _, _) -> True
_ -> False
)

result = Elixir.Enum.join([first_number, Elixir.Map.get(state, "result")])
result = Elixir.String.replace(result, "_", "")

state = case:
Elixir.String.contains?(result, '.') ->
Expand Down
Loading

0 comments on commit 06e8ad1

Please sign in to comment.