Skip to content

Commit

Permalink
feat: basic compile working
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Jan 10, 2023
1 parent 619ee7e commit f2a30fe
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 743 deletions.
67 changes: 67 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'cairo-lang-dojo'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=cairo-lang-dojo"
],
"filter": {
"name": "cairo-lang-dojo",
"kind": "lib"
}
},
"args": [],
"env": {
"CARGO_MANIFEST_DIR": "${workspaceFolder}/crates/cairo-lang-dojo"
},
"cwd": "${workspaceFolder}/crates/cairo-lang-dojo"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'dojo-compile'",
"cargo": {
"args": [
"build",
"--bin=dojo-compile",
"--package=cairo-lang-dojo"
],
"filter": {
"name": "dojo-compile",
"kind": "bin"
}
},
"args": ["/Users/tarrence/code/dojo/crates/cairo-lang-dojo/src/cairo_level_tests/component.cairo"],
"cwd": "${workspaceFolder}/crates/cairo-lang-dojo"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'dojo-compile'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=dojo-compile",
"--package=cairo-lang-dojo"
],
"filter": {
"name": "dojo-compile",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}/crates/cairo-lang-dojo"
}
]
}
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ license-file = "LICENSE"
[workspace.dependencies]
env_logger = "0.9.3"
indoc = "1.0.7"
itertools = "0.10.3"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0"
test-log = "0.2.11"
5 changes: 5 additions & 0 deletions crates/cairo-lang-dojo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ description = "Dojo capabilities and utilities on top of Starknet."
[dependencies]
anyhow = "1.0.66"
clap = { version = "4.0", features = ["derive"] }
cairo-lang-compiler = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-defs = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-diagnostics = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-lowering = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-parser = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-plugins = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-semantic = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-sierra-generator = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-syntax = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo.git", version = "0.1.0" }
indoc.workspace = true
itertools.workspace = true
serde.workspace = true
serde_json.workspace = true

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[crate_roots]
cairo_level_tests = "."
6 changes: 6 additions & 0 deletions crates/cairo-lang-dojo/src/cairo_level_tests/component.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Component<T> {
fn get<T>(entity_id: felt) -> T;
}

#[derive(Component)]
struct Position { x: felt, y: felt }
34 changes: 28 additions & 6 deletions crates/cairo-lang-dojo/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::fs;
use std::path::PathBuf;
use std::path::{PathBuf, Path};

use anyhow::Context;
use cairo_lang_starknet::contract_class::compile_path;
use cairo_lang_compiler::diagnostics::check_and_eprint_diagnostics;
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_compiler::project::setup_project;
use cairo_lang_diagnostics::ToOption;
use cairo_lang_dojo::db::get_dojo_database;
use clap::Parser;

/// Command line args parser.
Expand All @@ -21,11 +25,29 @@ struct Args {

fn main() -> anyhow::Result<()> {
let args = Args::parse();
let contract = compile_path(&PathBuf::from(args.path), args.replace_ids)?;
let res = serde_json::to_string_pretty(&contract).with_context(|| "Serialization failed.")?;
let path = &PathBuf::from(args.path);

let mut db_val = get_dojo_database();
let db = &mut db_val;

let main_crate_ids = setup_project(db, Path::new(&path))?;

if check_and_eprint_diagnostics(db) {
anyhow::bail!("Failed to compile: {}", path.display());
}

let sierra_program = db
.get_sierra_program(main_crate_ids)
.to_option()
.context("Compilation failed without any diagnostics")?;


// let contract = compile_path(path, args.replace_ids)?;
match args.output {
Some(path) => fs::write(path, res).with_context(|| "Failed to write output.")?,
None => println!("{}", res),
Some(path) => {
fs::write(path, format!("{}", sierra_program)).context("Failed to write output.")?
}
None => println!("{}", sierra_program),
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-dojo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
//! contracts on a permissionless Layer 2 network, secured by Ethereum using validity proofs.
//!
//! Learn more at [starkware.io](http://starknet.io/).
pub mod db;
pub mod plugin;
Loading

0 comments on commit f2a30fe

Please sign in to comment.