Skip to content

Commit

Permalink
Merge pull request #1221 from andrew-johnson-4/c-ansi-frontend-cejpof
Browse files Browse the repository at this point in the history
C ansi frontend cejpof
  • Loading branch information
andrew-johnson-4 authored Feb 6, 2025
2 parents 994c830 + 40cb4c5 commit 7f3a422
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@

dev: install-production
lm tests/c/main.c
lm tests/c/c-parse.lsts
cc -O3 tmp.c
./a.out
#lm tests/c/main.c
#cc -O3 tmp.c
#./a.out

build: compile-production
time ./production --c -o deploy1.c SRC/index-index.lm
Expand Down
12 changes: 12 additions & 0 deletions PLATFORM/C/LIB/maybe.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ let .get-or(m: Maybe<x>, default: x): x = (
}
);

let .get-or-panic(m: Maybe<x>): x = (
match m {
Some{content=content} => content;
}
);

let .is-none(m: Maybe<x>): U64 = (
match m {
Some{} => false;
Expand All @@ -22,3 +28,9 @@ let .is-some(m: Maybe<x>): U64 = (
None{} => false;
}
);

let cmp(l: Maybe<x>, r: Maybe<x>): Ord = (
if $".0"(l) != $".0"(r) then cmp($".0"(l), $".0"(r))
else if l.is-some then cmp(l.get-or-panic, r.get-or-panic)
else Equal
);
45 changes: 34 additions & 11 deletions PLUGINS/FRONTEND/C/c-parse.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ let std-c-parse(tokens: List<Token>): Nil = (
# while non-zero(tokens) { tokens = std-c-parse-external-declaration(tokens); }
);

let std-c-has-class(tk: CString, cls: String): U64 = (
tk == cls
);

let std-c-can-take(tokens: List<Token>, cls: String): U64 = (
non-zero(tokens) && std-c-has-class(head(tokens).key, cls)
);

let std-c-take-expect(tokens: List<Token>, cls: String): List<Token> = (
if non-zero(tokens) && std-c-has-class(head(tokens).key, cls) then tail(tokens)
else (print("Expected token [\{cls}] at \{tokens.formatted-location}\n"); exit(1); tokens);
);

let std-c-take-maybe(tokens: List<Token>, cls: String): List<Token> = (
if non-zero(tokens) && std-c-has-class(head(tokens).key, cls) then tail(tokens)
else tokens;
);

#let std-c-parse-external-declaration(tokens: List<Token>): Nil = (
# tokens = std-c-parse-function-definition();
# tokens = std-c-parse-declaration();
Expand Down Expand Up @@ -348,17 +366,22 @@ let std-c-parse(tokens: List<Token>): Nil = (
#struct-declarator = ':', constant-expression
# | declarator, [':', constant-expression];

#assignment-operator = '='
# | '*='
# | '/='
# | '%='
# | '+='
# | '-='
# | '<<='
# | '>>='
# | '&='
# | '^='
# | '|=';
let std-c-parse-assignment-operator(tokens: List<Token>): Tuple<Maybe<CString>,List<Token>> = (
let no = None :: Maybe<CString>;
if not(non-zero(tokens)) then Tuple{ no, tokens }
else if head(tokens).key == c"=" then Tuple{ Some{c"="}, tail(tokens) }
else if head(tokens).key == c"*=" then Tuple{ Some{c"*="}, tail(tokens) }
else if head(tokens).key == c"/=" then Tuple{ Some{c"/="}, tail(tokens) }
else if head(tokens).key == c"%=" then Tuple{ Some{c"%="}, tail(tokens) }
else if head(tokens).key == c"+=" then Tuple{ Some{c"+="}, tail(tokens) }
else if head(tokens).key == c"-=" then Tuple{ Some{c"-="}, tail(tokens) }
else if head(tokens).key == c"<<=" then Tuple{ Some{c"<<="}, tail(tokens) }
else if head(tokens).key == c">>=" then Tuple{ Some{c">>="}, tail(tokens) }
else if head(tokens).key == c"&=" then Tuple{ Some{c"&="}, tail(tokens) }
else if head(tokens).key == c"^=" then Tuple{ Some{c"^="}, tail(tokens) }
else if head(tokens).key == c"|=" then Tuple{ Some{c"|="}, tail(tokens) }
else Tuple{ no, tokens }
);

#parameter-list = parameter-declaration, {',', parameter-declaration};

Expand Down
1 change: 1 addition & 0 deletions PLUGINS/FRONTEND/C/c-smart-tokenize.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let std-c-tokenize-string(file-path: String, text: String): List<Token> = (
"/=".. rest => (tokens = cons(text[:"/=".length], tokens); text = rest;);
"%=".. rest => (tokens = cons(text[:"%=".length], tokens); text = rest;);
"&=".. rest => (tokens = cons(text[:"&=".length], tokens); text = rest;);
"^=".. rest => (tokens = cons(text[:"^=".length], tokens); text = rest;);
"|=".. rest => (tokens = cons(text[:"|=".length], tokens); text = rest;);
"<=".. rest => (tokens = cons(text[:"<=".length], tokens); text = rest;);
">=".. rest => (tokens = cons(text[:">=".length], tokens); text = rest;);
Expand Down
1 change: 1 addition & 0 deletions PLUGINS/FRONTEND/C/index-index.lm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import PLUGINS/FRONTEND/C/c-parse-all.lm;
import PLUGINS/FRONTEND/C/c-tokenize.lm;
import PLUGINS/FRONTEND/C/c-parse.lm;
import PLUGINS/FRONTEND/C/c-parse.lsts;
import PLUGINS/FRONTEND/C/c-tokenize.lsts;
import PLUGINS/FRONTEND/C/c-frontend.lsts;
import PLUGINS/FRONTEND/C/c-smart-tokenize.lsts;
1 change: 1 addition & 0 deletions SRC/index-index.lm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SRC/unit-fragments.lsts;
import SRC/unit-globals.lsts;
import SRC/unit-error.lsts;
import SRC/unit-preprocess.lsts;
import SRC/unit-orphans.lsts;

import SRC/index-definitions.lm;
import SRC/index-globals.lm;
Expand Down
2 changes: 1 addition & 1 deletion SRC/infer-expr.lm
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ infer-expr-one := λ(: tctx Maybe<TContext>)(: term AST)(: scoped IsScoped)(: hi
(let prev-tt (typeof-var-raw( term tctx lname )))
(if (non-zero prev-tt) (
(if (.is-t( prev-tt 'LocalVariable_s )) (
(exit-error( 'Variable\sName\sIs\sAlready\sBound\sIn\sOuter\sScope_s term ))
(exit-error( (+( 'Variable\sName\sIs\sAlready\sBound\sIn\sOuter\sScope\s_s lname )) term ))
) ())
) ())
(match rhs (
Expand Down
3 changes: 3 additions & 0 deletions SRC/unit-ast.lsts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

# dependencies
import SRC/unit-types.lsts;

# type definitions
import SRC/ast-definitions.lsts;
import SRC/ast-location.lsts;
Expand Down
4 changes: 0 additions & 4 deletions SRC/unit-drivers.lsts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@

import SRC/plugins-frontends.lsts;
import SRC/plugins-backends.lsts;

# orphans
import SRC/typeof-var.lsts;
import SRC/bind-varargs.lsts;
1 change: 1 addition & 0 deletions SRC/unit-globals.lsts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ import SRC/global-type-context.lsts;
# queries dependent on index information
import SRC/with-only-class.lsts;

import SRC/index-globals.lm;
8 changes: 8 additions & 0 deletions SRC/unit-orphans.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import SRC/typeof-var.lsts;
import SRC/bind-varargs.lsts;
import SRC/index-types.lm;
import PLUGINS/FRONTEND/LM/index-index.lm;
import SRC/index-plugins.lm;
import SRC/index-tokenize.lm;
import SRC/substitute.lsts;
72 changes: 72 additions & 0 deletions tests/c/c-parse.lsts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

import LIB/default.lsts;
import SRC/unit-ast.lsts;
import SRC/unit-drivers.lsts;
import SRC/unit-globals.lsts;
import SRC/unit-inference.lsts;
import SRC/unit-fragments.lsts;
import SRC/unit-orphans.lsts;
import SRC/unit-error.lsts;
import PLUGINS/FRONTEND/C/index-index.lm;
import PLUGINS/FRONTEND/C/index-index.lm;

# helpers
if true then {
let tokens = std-c-tokenize-string("[+]", "+");
assert( tokens.length == 1 );
assert( is(tokens, std-c-take-maybe(tokens, "-")) );
assert( std-c-can-take(tokens, "-") == false );
assert( std-c-can-take(tokens, "+") == true );
assert( is(tail(tokens), std-c-take-expect(tokens, "+")) );
assert( is(tail(tokens), std-c-take-maybe(tokens, "+")) );
};

# assignment operators
if true then {
let abc = std-c-tokenize-string("abc", "abc");
assert( std-c-parse-assignment-operator(abc).first == None :: Maybe<CString> );
if true then {
let tokens = std-c-tokenize-string("[=]", "=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"="} );
};
if true then {
let tokens = std-c-tokenize-string("[*=]", "*=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"*="} );
};
if true then {
let tokens = std-c-tokenize-string("[/=]", "/=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"/="} );
};
if true then {
let tokens = std-c-tokenize-string("[%=]", "%=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"%="} );
};
if true then {
let tokens = std-c-tokenize-string("[+=]", "+=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"+="} );
};
if true then {
let tokens = std-c-tokenize-string("[-=]", "-=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"-="} );
};
if true then {
let tokens = std-c-tokenize-string("[<<=]", "<<=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"<<="} );
};
if true then {
let tokens = std-c-tokenize-string("[>>=]", ">>=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c">>="} );
};
if true then {
let tokens = std-c-tokenize-string("[&=]", "&=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"&="} );
};
if true then {
let tokens = std-c-tokenize-string("[^=]", "^=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"^="} );
};
if true then {
let tokens = std-c-tokenize-string("[|=]", "|=");
assert( std-c-parse-assignment-operator(tokens).first == Some{c"|="} );
};
};
Empty file added tests/c/c-parse.lsts.out
Empty file.
Empty file added tests/c/c-parse.lsts.skip
Empty file.

0 comments on commit 7f3a422

Please sign in to comment.