Skip to content

Commit

Permalink
feat: self hosted tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjercan committed May 24, 2024
1 parent 367d9fd commit 76ac755
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ examples: all
./coolc examples/coin/client.cl examples/coin/message.cl --module net --module threading --module raylib -o build/coin-client

compiler: all
./coolc examples/compiler.cl --module prelude --module data -o build/compiler
./coolc examples/compiler/lexer.cl examples/compiler/compiler.cl --module prelude --module data -o build/cool-lexer

dist: clean all
rm -rf coolc.tar.gz
Expand Down
38 changes: 37 additions & 1 deletion checker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BUILD_DIR=build
TESTS_DIR=tests
TOTAL_TESTS=0
PASSED_TESTS=0
COOLC_SELF=$BUILD_DIR/cool

analyzer() {
if [ "$#" -ne 2 ]; then
Expand Down Expand Up @@ -138,10 +139,45 @@ librunner() {
PASSED_TESTS=$((PASSED_TESTS + passed))
}

self_hosted_lexical_analyzer() {
echo "Testing the self hosted lexical analyzer"
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <tests_dir>"
exit 1
fi

tests_dir=$TESTS_DIR/$1

echo "Running tests for $1"

passed=0
for file_path in $(ls $tests_dir/*.cl); do
ref_path=$tests_dir/$(basename $file_path .cl).ref

file_name=$(basename $file_path)
echo -en "Testing $file_name ... "

cat $file_path | ./$COOLC_SELF-lexer 2>&1 | diff - $ref_path > /dev/null 2>&1

if [ $? -eq 0 ]; then
echo -e "\e[32mPASSED\e[0m"
passed=$((passed + 1))
else
echo -e "\e[31mFAILED\e[0m"
fi
done

total=$(ls $tests_dir/*.cl | wc -l)
echo "Passed $passed/$total tests"

TOTAL_TESTS=$((TOTAL_TESTS + total))
PASSED_TESTS=$((PASSED_TESTS + passed))
}

lexical_analyzer() {
echo "Testing the lexical analyzer"
analyzer lexer --lex
self_hosted_lexical_analyzer lexer
}

syntax_analyzer() {
Expand Down Expand Up @@ -170,7 +206,7 @@ lib_tests() {
librunner lib
}

make clean && make
make clean && make compiler

ARG1=$1
if [ "$ARG1" == "--lex" ]; then
Expand Down
48 changes: 0 additions & 48 deletions examples/compiler.cl → examples/compiler/compiler.cl
Original file line number Diff line number Diff line change
Expand Up @@ -504,51 +504,3 @@ class Tokenizer {
}
};
};

class Main {
main(): Object {
let text: String <- new IO.in_string(),
tokens: List (* Token *) <- new Tokenizer.init(text).tokenize(),
iter: List <- tokens
in
while not isvoid iter loop
{
case iter.value() of
t: Token =>
{
new IO.out_string(t.to_string());
case t of
t: TokenIllegal => {
new IO.out_string("(").out_string(t.error().to_string());
case t.value() of
v: None => "";
v: Some => case v.value() of
v: String => {
new IO.out_string(" ").out_string(v);
};
esac;
esac;
new IO.out_string(")");
};
t: Token => {
case t.value() of
v: None => "";
v: Some => case v.value() of
v: String => {
new IO.out_string("(").out_string(v).out_string(")");
};
esac;
esac;
};
esac;

new IO.out_string("\n");
};
o: Object => abort();
esac;

iter <- iter.next();
}
pool
};
};
47 changes: 47 additions & 0 deletions examples/compiler/lexer.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Main {
main(): Object {
let text: String <- new IO.in_string(),
tokens: List (* Token *) <- new Tokenizer.init(text).tokenize(),
iter: List <- tokens
in
while not isvoid iter loop
{
case iter.value() of
t: Token =>
{
new IO.out_string(t.to_string());
case t of
t: TokenIllegal => {
new IO.out_string("(").out_string(t.error().to_string());
case t.value() of
v: None => "";
v: Some => case v.value() of
v: String => {
new IO.out_string(" ").out_string(v);
};
esac;
esac;
new IO.out_string(")");
};
t: Token => {
case t.value() of
v: None => "";
v: Some => case v.value() of
v: String => {
new IO.out_string("(").out_string(v).out_string(")");
};
esac;
esac;
};
esac;

new IO.out_string("\n");
};
o: Object => abort();
esac;

iter <- iter.next();
}
pool
};
};

0 comments on commit 76ac755

Please sign in to comment.