Browse Source

Add builtin functions

github-actions
Garrit Franke 2 years ago
parent
commit
b3c8c7c378
  1. 62
      Cargo.lock
  2. 3
      Cargo.toml
  3. 2
      examples/playground.sb
  4. 3
      src/builtin/README.md
  5. 7
      src/builtin/builtin.js
  6. 5
      src/builtin/mod.rs
  7. 10
      src/generator/js.rs
  8. 3
      src/lib/stdio.sb
  9. 2
      src/main.rs

62
Cargo.lock generated

@ -113,13 +113,55 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rust-embed"
version = "5.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a9619e0b88f073e59df757c75841f05568e92057e992971288d4cef5e12a178"
dependencies = [
"rust-embed-impl",
"rust-embed-utils",
"walkdir",
]
[[package]]
name = "rust-embed-impl"
version = "5.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6168c9daefd8dd3a1cf0e06a5f92a42537dc207f09cc6526e731dcfda979470e"
dependencies = [
"quote",
"rust-embed-utils",
"syn",
"walkdir",
]
[[package]]
name = "rust-embed-utils"
version = "5.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a512219132473ab0a77b52077059f1c47ce4af7fbdc94503e9862a34422876d"
dependencies = [
"walkdir",
]
[[package]]
name = "sabre"
version = "0.0.1"
dependencies = [
"rust-embed",
"structopt",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "strsim"
version = "0.8.0"
@ -200,6 +242,17 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "walkdir"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -216,6 +269,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

3
Cargo.toml

@ -7,4 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
structopt = "0.3.21"
structopt = "0.3.21"
rust-embed = "5.7.0"

2
examples/playground.sb

@ -1,5 +1,5 @@
fn main() {
let x = 10
x = 5
return x
_printf("Hello World!\n")
}

3
src/builtin/README.md

@ -0,0 +1,3 @@
The following function signatures need to be implemented by each backend:
\_printf(msg String)

7
src/builtin/builtin.js

@ -0,0 +1,7 @@
/* START builtins */
function _printf(msg) {
process.stdout.write(msg);
}
/* END builtins */

5
src/builtin/mod.rs

@ -0,0 +1,5 @@
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "src/builtin/"]
pub struct Builtins;

10
src/generator/js.rs

@ -20,12 +20,20 @@ pub struct JsGenerator;
impl Generator for JsGenerator {
fn generate(prog: Program) -> String {
let mut code = prog
let mut code = String::new();
let raw_builtins = crate::builtin::Builtins::get("builtin.js")
.expect("Could not locate builtin functions");
code += std::str::from_utf8(raw_builtins.as_ref())
.expect("Unable to interpret builtin functions");
let funcs: String = prog
.func
.into_iter()
.map(|f| generate_function(f))
.collect();
code += &funcs;
// Until we have a stdlib, it should suffice to print the result of main to stdout
code += "console.log(main())";

3
src/lib/stdio.sb

@ -0,0 +1,3 @@
fn print(msg) {
_printf(msg + "\n")
}

2
src/main.rs

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
extern crate rust_embed;
extern crate structopt;
use crate::generator::Generator;
@ -23,6 +24,7 @@ use std::io::Write;
use std::path::PathBuf;
use structopt::StructOpt;
mod builtin;
mod generator;
mod lexer;
mod parser;

Loading…
Cancel
Save