Skip to content

Commit

Permalink
Merge pull request #914 from andrew-johnson-4/parse-c-headers
Browse files Browse the repository at this point in the history
Parse c headers
  • Loading branch information
andrew-johnson-4 authored Nov 8, 2024
2 parents fdbc7bb + 9e0a935 commit d925f5f
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lambda_mountain"
version = "1.18.8"
version = "1.18.9"
authors = ["Andrew <andrew@subarctic.org>"]
license = "MIT"
description = "Typed Macro Assembler (backed by Coq proofs-of-correctness)"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

dev: install-production
lm --c t.lm
lm --c tests/regress/import-c.lm
cc tmp.c
./a.out
echo $?
Expand Down
2 changes: 1 addition & 1 deletion PLUGINS/FRONTEND/C/c-parse-all.lm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ c-parse-all := λ(: fps List<String>). (: (
) (
(wait(as (null()) U32[]))
))
(print(read-file(as tmp String)))
(c-parse(c-tokenize(read-file(as tmp String))))
) Nil);


4 changes: 4 additions & 0 deletions PLUGINS/FRONTEND/C/c-parse.lm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

c-parse := λ(: tokens List<String>). (: (
(print tokens)
) Nil);
142 changes: 142 additions & 0 deletions PLUGINS/FRONTEND/C/c-tokenize.lm
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@

c-tokenize := λ(: text String). (: (
(print text)
(let tokens (: LEOF List<String>))
(while (head-string text) (
(let prev-text text)
# these steps don't need to be ordered, so first-come first-serve
(if (has-prefix( text '\s_s )) (set text (remove-prefix( text '\s_s ))) ())
(if (has-prefix( text '\t_s )) (set text (remove-prefix( text '\t_s ))) ())
(if (has-prefix( text '\n_s )) (set text (remove-prefix( text '\n_s ))) ())
(if (has-prefix( text '\[_s )) ( (set text (remove-prefix( text '\[_s ))) (set tokens (cons( '\[_s tokens ))) ) ())
(if (has-prefix( text '\]_s )) ( (set text (remove-prefix( text '\]_s ))) (set tokens (cons( '\]_s tokens ))) ) ())
(if (has-prefix( text '[_s )) ( (set text (remove-prefix( text '[_s ))) (set tokens (cons( '[_s tokens ))) ) ())
(if (has-prefix( text ']_s )) ( (set text (remove-prefix( text ']_s ))) (set tokens (cons( ']_s tokens ))) ) ())
(if (has-prefix( text '{_s )) ( (set text (remove-prefix( text '{_s ))) (set tokens (cons( '{_s tokens ))) ) ())
(if (has-prefix( text '}_s )) ( (set text (remove-prefix( text '}_s ))) (set tokens (cons( '}_s tokens ))) ) ())
(if (has-prefix( text ',_s )) ( (set text (remove-prefix( text ',_s ))) (set tokens (cons( ',_s tokens ))) ) ())
(if (has-prefix( text '->_s )) ( (set text (remove-prefix( text '->_s ))) (set tokens (cons( '->_s tokens ))) ) ())
(if (has-prefix( text '!=_s )) ( (set text (remove-prefix( text '!=_s ))) (set tokens (cons( '!=_s tokens ))) ) ())
(if (has-prefix( text '!_s )) ( (set text (remove-prefix( text '!_s ))) (set tokens (cons( '!_s tokens ))) ) ())
(if (has-prefix( text '==_s )) ( (set text (remove-prefix( text '==_s ))) (set tokens (cons( '==_s tokens ))) ) ())
(if (has-prefix( text '=_s )) ( (set text (remove-prefix( text '=_s ))) (set tokens (cons( '=_s tokens ))) ) ())
(if (has-prefix( text '+=_s )) ( (set text (remove-prefix( text '+=_s ))) (set tokens (cons( '+=_s tokens ))) ) ())
(if (has-prefix( text '-=_s )) ( (set text (remove-prefix( text '-=_s ))) (set tokens (cons( '-=_s tokens ))) ) ())
(if (has-prefix( text '/=_s )) ( (set text (remove-prefix( text '/=_s ))) (set tokens (cons( '/=_s tokens ))) ) ())
(if (has-prefix( text '%=_s )) ( (set text (remove-prefix( text '%=_s ))) (set tokens (cons( '%=_s tokens ))) ) ())
(if (has-prefix( text '*=_s )) ( (set text (remove-prefix( text '*=_s ))) (set tokens (cons( '*=_s tokens ))) ) ())
(if (has-prefix( text '<<=_s )) ( (set text (remove-prefix( text '<<=_s ))) (set tokens (cons( '<<=_s tokens ))) ) ())
(if (has-prefix( text '>>=_s )) ( (set text (remove-prefix( text '>>=_s ))) (set tokens (cons( '>>=_s tokens ))) ) ())
(if (has-prefix( text '&=_s )) ( (set text (remove-prefix( text '&=_s ))) (set tokens (cons( '&=_s tokens ))) ) ())
(if (has-prefix( text '^=_s )) ( (set text (remove-prefix( text '^=_s ))) (set tokens (cons( '^=_s tokens ))) ) ())
(if (has-prefix( text '|=_s )) ( (set text (remove-prefix( text '|=_s ))) (set tokens (cons( '|=_s tokens ))) ) ())
(if (has-prefix( text '+_s )) ( (set text (remove-prefix( text '+_s ))) (set tokens (cons( '+_s tokens ))) ) ())
(if (has-prefix( text '-_s )) ( (set text (remove-prefix( text '-_s ))) (set tokens (cons( '-_s tokens ))) ) ())
(if (has-prefix( text '*_s )) ( (set text (remove-prefix( text '*_s ))) (set tokens (cons( '*_s tokens ))) ) ())
(if (has-prefix( text '/_s )) ( (set text (remove-prefix( text '/_s ))) (set tokens (cons( '/_s tokens ))) ) ())
(if (has-prefix( text '%_s )) ( (set text (remove-prefix( text '%_s ))) (set tokens (cons( '%_s tokens ))) ) ())
(if (has-prefix( text '<<_s )) ( (set text (remove-prefix( text '<<_s ))) (set tokens (cons( '<<_s tokens ))) ) ())
(if (has-prefix( text '>>_s )) ( (set text (remove-prefix( text '>>_s ))) (set tokens (cons( '>>_s tokens ))) ) ())
(if (has-prefix( text '&_s )) ( (set text (remove-prefix( text '&_s ))) (set tokens (cons( '&_s tokens ))) ) ())
(if (has-prefix( text '^_s )) ( (set text (remove-prefix( text '^_s ))) (set tokens (cons( '^_s tokens ))) ) ())
(if (has-prefix( text '|_s )) ( (set text (remove-prefix( text '|_s ))) (set tokens (cons( '|_s tokens ))) ) ())
(if (has-prefix( text '~_s )) ( (set text (remove-prefix( text '~_s ))) (set tokens (cons( '~_s tokens ))) ) ())
(if (has-prefix( text '?_s )) ( (set text (remove-prefix( text '?_s ))) (set tokens (cons( '?_s tokens ))) ) ())
(if (has-prefix( text ':_s )) ( (set text (remove-prefix( text ':_s ))) (set tokens (cons( ':_s tokens ))) ) ())
(if (has-prefix( text '\:_s )) ( (set text (remove-prefix( text '\:_s ))) (set tokens (cons( '\:_s tokens ))) ) ())
(if (has-prefix( text '._s )) ( (set text (remove-prefix( text '._s ))) (set tokens (cons( '._s tokens ))) ) ())
(if (has-prefix( text '\o_s )) (
(while (&&( (!=( (head-string text) 0_u8 )) (!=( (head-string text) 10_u8 )) )) (
(set text (tail-string text))
))
) ())
(if (is-c-ident-char( text )) (
(let t SNil)
(while (&&( (!=( (head-string text) 0_u8 )) (is-c-ident-char text) )) (
(set t (+( t (SAtom(clone-rope(head-string text))) )))
(set text (tail-string text))
))
(set tokens (cons( (clone-rope t) tokens )))
) ())
(if (==( (as text U64) (as prev-text U64) )) (
(print 'Unrecognized\sC\sToken\sStart:\s_s)(print (head-string text))(print '\n_s)(exit 1_u64)
) ())
))
tokens
) List<String>);

is-c-ident-char := λ(: text String). (: (
(let r 0_u64)
(match (head-string text) (
()
( 46_u8 (set r 1_u64) )
( 48_u8 (set r 1_u64) )
( 49_u8 (set r 1_u64) )
( 50_u8 (set r 1_u64) )
( 51_u8 (set r 1_u64) )
( 52_u8 (set r 1_u64) )
( 53_u8 (set r 1_u64) )
( 54_u8 (set r 1_u64) )
( 55_u8 (set r 1_u64) )
( 56_u8 (set r 1_u64) )
( 57_u8 (set r 1_u64) )
( 65_u8 (set r 1_u64) )
( 66_u8 (set r 1_u64) )
( 67_u8 (set r 1_u64) )
( 68_u8 (set r 1_u64) )
( 69_u8 (set r 1_u64) )
( 70_u8 (set r 1_u64) )
( 71_u8 (set r 1_u64) )
( 72_u8 (set r 1_u64) )
( 73_u8 (set r 1_u64) )
( 74_u8 (set r 1_u64) )
( 75_u8 (set r 1_u64) )
( 76_u8 (set r 1_u64) )
( 77_u8 (set r 1_u64) )
( 78_u8 (set r 1_u64) )
( 79_u8 (set r 1_u64) )
( 80_u8 (set r 1_u64) )
( 81_u8 (set r 1_u64) )
( 82_u8 (set r 1_u64) )
( 83_u8 (set r 1_u64) )
( 84_u8 (set r 1_u64) )
( 85_u8 (set r 1_u64) )
( 86_u8 (set r 1_u64) )
( 87_u8 (set r 1_u64) )
( 88_u8 (set r 1_u64) )
( 89_u8 (set r 1_u64) )
( 90_u8 (set r 1_u64) )
( 95_u8 (set r 1_u64) )
( 97_u8 (set r 1_u64) )
( 98_u8 (set r 1_u64) )
( 99_u8 (set r 1_u64) )
( 100_u8 (set r 1_u64) )
( 101_u8 (set r 1_u64) )
( 102_u8 (set r 1_u64) )
( 103_u8 (set r 1_u64) )
( 104_u8 (set r 1_u64) )
( 105_u8 (set r 1_u64) )
( 106_u8 (set r 1_u64) )
( 107_u8 (set r 1_u64) )
( 108_u8 (set r 1_u64) )
( 109_u8 (set r 1_u64) )
( 110_u8 (set r 1_u64) )
( 111_u8 (set r 1_u64) )
( 112_u8 (set r 1_u64) )
( 113_u8 (set r 1_u64) )
( 114_u8 (set r 1_u64) )
( 115_u8 (set r 1_u64) )
( 116_u8 (set r 1_u64) )
( 117_u8 (set r 1_u64) )
( 118_u8 (set r 1_u64) )
( 119_u8 (set r 1_u64) )
( 120_u8 (set r 1_u64) )
( 121_u8 (set r 1_u64) )
( 122_u8 (set r 1_u64) )
( _ () )
))
r
) U64);



2 changes: 2 additions & 0 deletions PLUGINS/FRONTEND/C/index-index.lm
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

import PLUGINS/FRONTEND/C/c-parse-all.lm;
import PLUGINS/FRONTEND/C/c-tokenize.lm;
import PLUGINS/FRONTEND/C/c-parse.lm;

0 comments on commit d925f5f

Please sign in to comment.