-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1289 from andrew-johnson-4/ansi-c-frontend-rqer
Ansi c frontend rqer
- Loading branch information
Showing
5 changed files
with
100 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
let std-c-declare(t: CTerm): Nil = ( | ||
match t { | ||
CFunctionDefinition{spec=specifiers,decl=declarator,dl=declaration-list,stmt=statement} => std-c-declare-function(spec, decl, dl, stmt); | ||
_ => fail("Unsupported C Declaration:\n\{t}\n"); | ||
}; | ||
); | ||
|
||
let std-c-declare-function(specifiers: CTerm, declarator: CTerm, declaration-list: CTerm, statement: CTerm): Nil = ( | ||
(let return-type, let misc-types) = std-c-type-of-specifiers(specifiers); | ||
(let name, let pointer) = std-c-name-of-declarator(declarator); | ||
let params = std-c-lhs-of-parameter-list(declaration-list); | ||
let body = std-c-expr-of-statement(statement); | ||
print("Name: \{name}\n"); | ||
print("Return Type: \{return-type}\n"); | ||
print("Misc Type: \{misc-types}\n"); | ||
exit(1); | ||
); | ||
|
||
let std-c-type-of-specifiers(specifiers: CTerm): Tuple<Type,Type> = ( | ||
let misc = TAny; | ||
let rtype = match specifiers { | ||
CList{value:[CType1{rv=value}..]} => t2(c"C", t1(untern(rv))); | ||
_ => fail("Unsupported C Specifiers:\n\{specifiers}\n"); | ||
}; | ||
(rtype, misc); | ||
); | ||
|
||
let std-c-name-of-declarator(declarator: CTerm): Tuple<String,Type> = ( | ||
match declarator { | ||
CUnaryPrefix{op:"Declarator(", arg:CIdentifier{name=value}} => (name, TAny); | ||
_ => fail("Unsupported C Declarator:\n\{declarator}\n"); | ||
}; | ||
); | ||
|
||
let std-c-lhs-of-parameter-list(declaration-list: CTerm): AST = ( | ||
match declaration-list { | ||
CMaybe{value:None{}} => ASTNil; | ||
_ => fail("Unsupported C Parameter List:\n\{declaration-list}\n"); | ||
}; | ||
); | ||
|
||
let std-c-expr-of-statement(t: CTerm): AST = ( | ||
match t { | ||
CCompound{terms=terms} => ( | ||
let inner = ASTNil; | ||
for it in terms { | ||
inner = App{ true, close(inner), close(std-c-expr-of-statement(it)) }; | ||
}; | ||
App{ false, close(Var{c"c::compound", mk-token(c"c::compound")}), close(inner) } | ||
); | ||
CUnaryPrefix{op:"return", arg=arg} => ( | ||
let inner = std-c-expr-of-statement(arg); | ||
App{ false, close(Var{c"c::return", mk-token(c"c::return")}), close(inner) } | ||
); | ||
CInteger{value=value} => ( | ||
App{ false, | ||
close(Var{c":", mk-token(c":")}), | ||
close(App{ | ||
close(Lit{ untern(value), mk-token(value) }), | ||
close(AType{ std-c-type-of-integer(value) }) | ||
}) | ||
} | ||
); | ||
_ => fail("Unsupported C Statement:\n\{t}\n"); | ||
}; | ||
); | ||
|
||
let std-c-type-of-integer(i: String): Type = ( | ||
if i.has-prefix("-") { | ||
let n = to-u64(untern(tail-string(i))); | ||
if n <= 128 then t2(c"C",t1(c"uint8_t")) else | ||
if n <= 32768 then t2(c"C",t1(c"uint16_t")) else | ||
if n <= 2147483648 then t2(c"C",t1(c"uint32_t")) else | ||
t2(c"C",t1(c"uint64_t")) | ||
} else { | ||
let n = to-u64(untern(i)); | ||
if n <= 255 then t2(c"C",t1(c"int8_t")) else | ||
if n <= 65535 then t2(c"C",t1(c"int16_t")) else | ||
if n <= 4294967295 then t2(c"C",t1(c"int32_t")) else | ||
t2(c"C",t1(c"int64_t")) | ||
}; | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
|
||
let c-frontend(fp: CString): Nil = ( | ||
let tokens = std-c-tokenize-string(fp, read-file(fp)); | ||
for t in tokens { | ||
print("token: \{t.key}\n"); | ||
}; | ||
print("TODO: parse \{fp}\n"); exit(1); | ||
std-c-parse(tokens); | ||
); | ||
|
||
register-frontend(c".c", c-frontend); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters