initial framework

master
Able 2022-02-10 18:28:59 -06:00
commit c3caaf692f
Signed by: able
GPG Key ID: D164AF5F5700BE51
9 changed files with 10568 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

93
Cargo.lock generated Normal file
View File

@ -0,0 +1,93 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "beef"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bed554bd50246729a1ec158d08aa3235d1b69d94ad120ebe187e28894787e736"
[[package]]
name = "cwasm"
version = "0.1.0"
dependencies = [
"logos",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "logos"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427e2abca5be13136da9afdbf874e6b34ad9001dd70f2b103b083a85daa7b345"
dependencies = [
"logos-derive",
]
[[package]]
name = "logos-derive"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56a7d287fd2ac3f75b11f19a1c8a874a7d55744bd91f7a1b3e7cf87d4343c36d"
dependencies = [
"beef",
"fnv",
"proc-macro2",
"quote",
"regex-syntax",
"syn",
"utf8-ranges",
]
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "syn"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "utf8-ranges"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "cwasm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
logos = "0.12.0"

10333
n1256.pdf Normal file

File diff suppressed because one or more lines are too long

103
src/c_tokens.rs Normal file
View File

@ -0,0 +1,103 @@
#[derive(PartialEq, Debug)]
pub enum IncludeType {
System(String),
Local(String),
}
#[derive(PartialEq, Debug, Logos)]
pub enum Tokens {
/// A signed integer literal.
#[regex("[-]?[0-9]+", parse_int, priority = 1)]
Int(i32),
#[regex("[-]?[0-9]+.[0-9]+", float_parse, priority = 2)]
Float(f32),
#[regex(";")]
SemiColon,
#[regex("[A-Za-z][A-Za-z0-9]+", parse_ident, priority = 3)]
Ident(String),
/*
// FIXME: I don't know how to properly parse this.
#[regex("\"[A-Za-z0-9]+\"", parse_string, priority = 123)]
StringLiteral(String),
*/
#[regex("#include[ \t]+<[^>]+>", include_parse)]
Include(IncludeType),
#[regex("return")]
Return,
#[token("(")]
LParen,
#[token(")")]
RParen,
#[token("{")]
LBrace,
#[token("}")]
RBrace,
#[regex(r"[ \t\n\f]+", logos::skip)]
#[error]
Error,
}
use logos::{Lexer, Logos};
fn parse_int(lex: &mut Lexer<Tokens>) -> Option<i32> {
let slice = lex.slice();
let int = slice.parse::<i32>().ok()?;
Some(int)
}
pub fn float_parse(lex: &mut Lexer<Tokens>) -> Option<f32> {
let slice = lex.slice();
let float = slice.parse::<f32>().ok()?;
Some(float)
}
fn include_parse(lex: &mut Lexer<Tokens>) -> Option<IncludeType> {
let slice = lex.slice();
let system_include_check = slice.starts_with("#include <");
let include = slice.to_string();
if system_include_check {
let ret = parse_inner_include(&include, "<", ">");
Some(IncludeType::System(ret))
} else {
let ret = parse_inner_include(&include, "\"", "\"");
Some(IncludeType::Local(ret))
}
}
fn parse_inner_include(include: &str, opener: &str, closer: &str) -> String {
let start_bytes = include.find(opener).unwrap_or(0); //index where "pattern" starts
let end_bytes = include.find(closer).unwrap_or(include.len()); //index where "<" is found
let ret = &include[start_bytes + 1..end_bytes];
ret.to_string()
}
pub fn parse_ident(lex: &mut Lexer<Tokens>) -> Option<String> {
let slice = lex.slice();
let ident = slice.to_string();
Some(ident)
}
pub fn parse_string(lex: &mut Lexer<Tokens>) -> Option<String> {
let slice = lex.slice();
let string = slice.to_string();
Some(string)
}

9
src/hosted/mod.rs Normal file
View File

@ -0,0 +1,9 @@
use std::fs::File;
use std::io::prelude::*;
pub fn write_to_disk() -> std::io::Result<()> {
let mut file = File::create("foo.wasm")?;
file.write_all(b"\0asm")?;
Ok(())
}

14
src/main.rs Normal file
View File

@ -0,0 +1,14 @@
pub mod c_tokens;
use c_tokens::Tokens;
use logos::Logos;
#[cfg(test)]
pub mod tests;
fn main() {
let lex = c_tokens::Tokens::lexer(include_str!("test.c"));
for token in lex {
println!("{:?}", token);
}
}

5
src/test.c Normal file
View File

@ -0,0 +1,5 @@
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}

1
src/tests.rs Normal file
View File

@ -0,0 +1 @@