Compare commits
5 Commits
42bc666c11
...
dsx-pkg
| Author | SHA1 | Date | |
|---|---|---|---|
| c02ecd58e8 | |||
| 0d54b319f1 | |||
| ba4ced6433 | |||
| 4bee36eb7f | |||
| 71b36dc6b5 |
@@ -1,3 +1,5 @@
|
||||
/target
|
||||
**/*.env
|
||||
Cargo.lock
|
||||
.test/
|
||||
pkg
|
||||
|
||||
@@ -29,6 +29,10 @@
|
||||
"command": "cargo build --release",
|
||||
"use_new_terminal": false,
|
||||
},
|
||||
{
|
||||
"label": "Publish Arch Package",
|
||||
"command": "sh resources/publish.sh",
|
||||
},
|
||||
{
|
||||
"label": "Run Tests",
|
||||
"command": "cargo test",
|
||||
|
||||
+5
-1
@@ -1,7 +1,11 @@
|
||||
cargo-features = ["codegen-backend"]
|
||||
|
||||
[workspace]
|
||||
members = ["emulator", "common", "assembler", "dsa_editor", "compiler", "dsx_server"]
|
||||
members = [
|
||||
"core/dsa_common", "core/assembler", "core/compiler",
|
||||
"emulator", "emulator/dsa_editor",
|
||||
"dsx/dsx_server", "dsx/dsx_common", "dsx/dsx"
|
||||
]
|
||||
resolver = "3"
|
||||
|
||||
[workspace.package]
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# Maintainer: zxq5 <zxq5@proton.me>
|
||||
pkgbase='damn-simple-architecture'
|
||||
pkgname=('dsa' 'dsx' 'dsa-tools' 'dsx-server')
|
||||
pkgver=0.1.1
|
||||
pkgrel=1
|
||||
startdir='.'
|
||||
pkgdesc="Damn Simple Architecture"
|
||||
arch=('x86_64')
|
||||
url="https://git.zxq5.dev/zxq5/damn-simple-architecture"
|
||||
license=('MIT')
|
||||
makedepends=('rust' 'cargo' 'sed')
|
||||
|
||||
build() {
|
||||
cargo build --release \
|
||||
--bin dsa \
|
||||
--bin dsx \
|
||||
--bin dsa-a \
|
||||
--bin dsa-c \
|
||||
--bin dsx-server
|
||||
}
|
||||
|
||||
package_dsa() {
|
||||
pkgdesc="DSA core binary"
|
||||
depends=()
|
||||
|
||||
install -Dm755 "$startdir/target/release/dsa" "$pkgdir/usr/bin/dsa"
|
||||
install -Dm644 "$startdir/resources/dsa.desktop" \
|
||||
"$pkgdir/usr/share/applications/dsa.desktop"
|
||||
install -Dm644 "$startdir/resources/dsa.png" \
|
||||
"$pkgdir/usr/share/icons/hicolor/256x256/apps/dsa.png"
|
||||
}
|
||||
|
||||
package_dsx() {
|
||||
pkgdesc="DSX client"
|
||||
depends=('dsa')
|
||||
|
||||
install -Dm755 "$startdir/target/release/dsx" "$pkgdir/usr/bin/dsx"
|
||||
}
|
||||
|
||||
package_dsa-tools() {
|
||||
pkgdesc="DSA assembler and compiler tools"
|
||||
depends=()
|
||||
|
||||
install -Dm755 "$startdir/target/release/dsa-a" "$pkgdir/usr/bin/dsa-a"
|
||||
install -Dm755 "$startdir/target/release/dsa-c" "$pkgdir/usr/bin/dsa-c"
|
||||
}
|
||||
|
||||
package_dsx-server() {
|
||||
pkgdesc="DSX server"
|
||||
depends=()
|
||||
|
||||
install -Dm755 "$startdir/target/release/dsx-server" "$pkgdir/usr/bin/dsx-server"
|
||||
|
||||
# Example sed usage — patch config paths for system install
|
||||
sed -i 's|./templates|/usr/share/dsx-server/templates|g' \
|
||||
"target/release/dsx-server" 2>/dev/null || true
|
||||
|
||||
install -Dm644 "$startdir/dsx_server/templates" "$pkgdir/usr/share/dsx-server/templates" 2>/dev/null || true
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
use crate::model::{CompilerError, Program};
|
||||
|
||||
mod codegen;
|
||||
mod instruction;
|
||||
mod registers;
|
||||
mod scope;
|
||||
|
||||
pub fn generate_code(ast: &Program) -> Result<String, CompilerError> {
|
||||
let mut codegen = codegen::CodeGenerator::new(ast.clone());
|
||||
codegen.generate()
|
||||
}
|
||||
@@ -5,7 +5,7 @@ edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "assembler"
|
||||
name = "dsa-a"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
@@ -13,6 +13,6 @@ name = "assembler"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../common" }
|
||||
common = { path = "../dsa_common" }
|
||||
num_cpus = "1.17.0"
|
||||
threadpool = "1.8.1"
|
||||
@@ -58,6 +58,7 @@ impl From<AssembleError> for BuildError {
|
||||
|
||||
impl Builder for Assembler {
|
||||
type Output = Vec<u8>;
|
||||
type Args = ();
|
||||
|
||||
fn logs(&self) -> Vec<String> {
|
||||
self.logs_rx.logs()
|
||||
@@ -78,7 +79,7 @@ impl Builder for Assembler {
|
||||
}
|
||||
|
||||
/// Start the compilation process in a separate thread
|
||||
fn start(&mut self) {
|
||||
fn start(&mut self, args: ()) {
|
||||
if self.is_running {
|
||||
return;
|
||||
}
|
||||
@@ -46,7 +46,7 @@ fn main() {
|
||||
let output_path = &args[4];
|
||||
|
||||
let mut engine = Assembler::new(PathBuf::from(input_path));
|
||||
engine.start();
|
||||
engine.start(());
|
||||
let result = engine.output().expect("assembler failed.");
|
||||
|
||||
if let Err(e) = fs::write(output_path, result) {
|
||||
@@ -4,7 +4,12 @@ version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "dsa-c"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.43"
|
||||
common = { path = "../common" }
|
||||
common = { path = "../dsa_common" }
|
||||
uuid = { version = "1.20.0", features = ["v4"] }
|
||||
@@ -20,10 +20,11 @@ pub struct CodeGenerator {
|
||||
functions: Vec<IB>,
|
||||
symbols: Vec<String>,
|
||||
allocator: RegisterAllocator,
|
||||
is_library: bool,
|
||||
}
|
||||
|
||||
impl CodeGenerator {
|
||||
pub fn new(ast: Program) -> Self {
|
||||
pub fn new(ast: Program, is_lib: bool) -> Self {
|
||||
CodeGenerator {
|
||||
ast,
|
||||
imports: HashMap::new(),
|
||||
@@ -31,6 +32,7 @@ impl CodeGenerator {
|
||||
functions: Vec::new(),
|
||||
symbols: Vec::new(),
|
||||
allocator: RegisterAllocator::new(),
|
||||
is_library: is_lib,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +48,9 @@ impl CodeGenerator {
|
||||
|
||||
pub fn generate(&mut self) -> Result<String, CompilerError> {
|
||||
// always include the print library for debugging!
|
||||
self.include("print", "./lib/io/print.dsa");
|
||||
if !self.is_library {
|
||||
self.include("print", "./lib/print.dsa");
|
||||
}
|
||||
|
||||
for block in self.ast.clone().declarations {
|
||||
match block {
|
||||
@@ -101,6 +105,7 @@ impl CodeGenerator {
|
||||
|
||||
block.extend(self.globals.values().cloned().collect::<Vec<_>>());
|
||||
|
||||
if !self.is_library {
|
||||
block.extend(vec![
|
||||
I::Newline,
|
||||
I::global_comment("Entry Point"),
|
||||
@@ -120,6 +125,10 @@ impl CodeGenerator {
|
||||
I::call("print::print_hex_word"),
|
||||
I::pop(Register::Zero),
|
||||
I::Hlt,
|
||||
]);
|
||||
}
|
||||
|
||||
block.extend(vec![
|
||||
I::Newline,
|
||||
// default return block boilerplate
|
||||
I::global_comment("Return"),
|
||||
@@ -0,0 +1,11 @@
|
||||
use crate::model::{CompilerError, Program};
|
||||
|
||||
mod codegen;
|
||||
mod instruction;
|
||||
mod registers;
|
||||
mod scope;
|
||||
|
||||
pub fn generate_code(ast: &Program, is_lib: bool) -> Result<String, CompilerError> {
|
||||
let mut codegen = codegen::CodeGenerator::new(ast.clone(), is_lib);
|
||||
codegen.generate()
|
||||
}
|
||||
@@ -2,9 +2,13 @@ use crate::model::{CompilerError, Program};
|
||||
|
||||
mod dsa;
|
||||
|
||||
pub fn compiler_backend(ext: &str, ast: &Program) -> Result<String, CompilerError> {
|
||||
pub fn compiler_backend(
|
||||
ext: &str,
|
||||
ast: &Program,
|
||||
is_lib: bool,
|
||||
) -> Result<String, CompilerError> {
|
||||
match ext {
|
||||
"dsa" => Ok(dsa::generate_code(ast)?),
|
||||
"dsa" => Ok(dsa::generate_code(ast, is_lib)?),
|
||||
_ => Err(CompilerError::Generic(format!(
|
||||
"File type {} not supported",
|
||||
ext
|
||||
@@ -722,37 +722,42 @@ impl Parser {
|
||||
|
||||
// if the next token isn't the beginning of a struct literal this is just
|
||||
// an identifier.
|
||||
if !expect_tt!(self.peek_next()?, LeftBrace).accepted() {
|
||||
return ParseResult::Accept(Expression::Variable {
|
||||
// if !expect_tt!(self.peek_next()?, LeftBrace).accepted() {
|
||||
// return ParseResult::Accept(Expression::Variable {
|
||||
// name,
|
||||
// expr_type: None,
|
||||
// });
|
||||
// }
|
||||
//
|
||||
ParseResult::Accept(Expression::Variable {
|
||||
name,
|
||||
expr_type: None,
|
||||
});
|
||||
}
|
||||
|
||||
let _ = self.next()?;
|
||||
|
||||
let mut fields = Vec::new();
|
||||
while !expect_tt!(self.peek_next()?, RightBrace).accepted() {
|
||||
let name = expect_value!(self.next()?, Identifier)?;
|
||||
let _ = expect_tt!(self.next()?, Colon)?;
|
||||
let expr = self.parse_expression()?;
|
||||
|
||||
fields.push((name, expr));
|
||||
|
||||
if expect_tt!(self.peek_next()?, Comma).accepted() {
|
||||
self.next()?;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let _ = expect_tt!(self.next()?, RightBrace)?;
|
||||
|
||||
ParseResult::Accept(Expression::StructLiteral {
|
||||
name,
|
||||
fields,
|
||||
type_id: None,
|
||||
})
|
||||
|
||||
// let _ = self.next()?;
|
||||
|
||||
// let mut fields = Vec::new();
|
||||
// while !expect_tt!(self.peek_next()?, RightBrace).accepted() {
|
||||
// let name = expect_value!(self.next()?, Identifier)?;
|
||||
// let _ = expect_tt!(self.next()?, Colon)?;
|
||||
// let expr = self.parse_expression()?;
|
||||
|
||||
// fields.push((name, expr));
|
||||
|
||||
// if expect_tt!(self.peek_next()?, Comma).accepted() {
|
||||
// self.next()?;
|
||||
// } else {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// let _ = expect_tt!(self.next()?, RightBrace)?;
|
||||
|
||||
// ParseResult::Accept(Expression::StructLiteral {
|
||||
// name,
|
||||
// fields,
|
||||
// type_id: None,
|
||||
// })
|
||||
}
|
||||
Token::LeftBracket => {
|
||||
self.next()?; // consume '['
|
||||
@@ -21,7 +21,7 @@ pub struct Compiler {
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
fn build(&mut self) -> Result<String, Box<dyn std::error::Error>> {
|
||||
fn build(&mut self, is_lib: bool) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let input =
|
||||
std::fs::read_to_string(&self.src_path).expect("Failed to read input file");
|
||||
|
||||
@@ -46,7 +46,7 @@ impl Compiler {
|
||||
// println!("Parsed AST: {:#?}", ast);
|
||||
|
||||
// Generate the output using the backend with the parsed result.
|
||||
let result = match backend::compiler_backend("dsa", &ast) {
|
||||
let result = match backend::compiler_backend("dsa", &ast, is_lib) {
|
||||
Ok(result) => result,
|
||||
Err(err) => return Err(format!("Compilation failed: {err:?}").into()),
|
||||
};
|
||||
@@ -57,6 +57,7 @@ impl Compiler {
|
||||
|
||||
impl Builder for Compiler {
|
||||
type Output = String;
|
||||
type Args = bool;
|
||||
|
||||
fn new(src_path: impl Into<PathBuf>) -> Self {
|
||||
Self {
|
||||
@@ -66,8 +67,8 @@ impl Builder for Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
fn start(&mut self) {
|
||||
match self.build() {
|
||||
fn start(&mut self, args: bool) {
|
||||
match self.build(args) {
|
||||
Ok(x) => self.result = Some(Ok(x)),
|
||||
Err(err) => self.result = Some(Err(err.into())),
|
||||
}
|
||||
@@ -7,7 +7,7 @@ fn main() {
|
||||
// read from input file: syntax "c_compiler <src.c> [output.dsa]"
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: c_compiler <src.dsc> [output.dsa]");
|
||||
eprintln!("Usage: c_compiler [--lib | --bin] <src.dsc> [output.dsa]");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -19,8 +19,10 @@ fn main() {
|
||||
};
|
||||
|
||||
{
|
||||
let is_lib = args.contains(&"--lib".to_string());
|
||||
|
||||
let mut builder = Compiler::new(PathBuf::from(input_file));
|
||||
builder.start();
|
||||
builder.start(is_lib);
|
||||
let result = builder.output().unwrap();
|
||||
|
||||
std::fs::write(output_file, &result).expect("Failed to write output");
|
||||
@@ -32,11 +32,12 @@ impl fmt::Display for BuildError {
|
||||
|
||||
pub trait Builder {
|
||||
type Output: Clone + std::convert::AsRef<[u8]>;
|
||||
type Args: Clone;
|
||||
|
||||
fn new(src_path: impl Into<PathBuf>) -> Self;
|
||||
|
||||
// starts compilation
|
||||
fn start(&mut self);
|
||||
fn start(&mut self, args: Self::Args);
|
||||
|
||||
// non-blocking function, returns output if completed
|
||||
fn poll(&mut self) -> Option<Result<Self::Output, BuildError>>;
|
||||
@@ -21,7 +21,7 @@ impl Logger {
|
||||
#[must_use]
|
||||
pub fn new(logs_tx: mpsc::Sender<String>, use_stdio: bool) -> Self {
|
||||
Self {
|
||||
use_stdio: true,
|
||||
use_stdio,
|
||||
logs_tx: Arc::new(logs_tx),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "dsx"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "dsx"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
compiler = { path = "../../core/compiler" }
|
||||
assembler = { path = "../../core/assembler" }
|
||||
common = { path = "../../core/dsa_common" }
|
||||
dsx_common = { path = "../dsx_common" }
|
||||
toml = "1.0.3"
|
||||
chrono = "0.4.44"
|
||||
reqwest = { version = "0.13.2", default-features = false, features = ["blocking", "native-tls"] }
|
||||
tar = "0.4.44"
|
||||
flate2 = "1.1.9"
|
||||
@@ -0,0 +1,120 @@
|
||||
use std::{env, fs, path::PathBuf, process::Command};
|
||||
|
||||
use dsx_common::builder::{self, BuildContext};
|
||||
use reqwest::Url;
|
||||
|
||||
pub mod new;
|
||||
pub mod repo;
|
||||
pub mod templates;
|
||||
|
||||
fn main() {
|
||||
// Very small CLI – only three sub‑commands.
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: dsx-build <new|build|package> [options]");
|
||||
std::process::exit(1);
|
||||
}
|
||||
match args[1].as_str() {
|
||||
"new" => new::new_project(&args[2..]),
|
||||
"build" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
let ctx = BuildContext {
|
||||
project_dir: dir.clone(),
|
||||
build_dir: dir.join("build"),
|
||||
artifact_dir: dir.join("artifacts"),
|
||||
};
|
||||
|
||||
builder::build_project(ctx).expect("Build failed!");
|
||||
} else {
|
||||
eprintln!("No Dsx.toml found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
"clean" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
fs::remove_dir_all(dir.join("build"))
|
||||
.expect("failed to remove build dir");
|
||||
println!("Build directory cleaned...");
|
||||
fs::remove_dir_all(dir.join("artifacts"))
|
||||
.expect("failed to remove artifacts dir");
|
||||
println!("Artifacts directory cleaned...");
|
||||
} else {
|
||||
eprintln!("No Dsx.toml found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
"run" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
let ctx = BuildContext {
|
||||
project_dir: dir.clone(),
|
||||
build_dir: dir.join("build"),
|
||||
artifact_dir: dir.join("artifacts"),
|
||||
};
|
||||
|
||||
builder::build_project(ctx).expect("Run failed!");
|
||||
|
||||
// start process and call emulator
|
||||
let mut child = Command::new("dsa")
|
||||
.arg("--cli")
|
||||
.arg("--bin")
|
||||
.arg(dir.join("./artifacts/out.dsb"))
|
||||
.spawn()
|
||||
.expect("Failed to start emulator");
|
||||
|
||||
// wait for emulator to finish
|
||||
child.wait().expect("Failed to wait for emulator");
|
||||
} else {
|
||||
eprintln!("No Dsx.toml found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
"clone" => {
|
||||
let url: Url = Url::parse(&args[2]).unwrap();
|
||||
let name = url.path_segments().unwrap().next_back().unwrap();
|
||||
let repo = env::current_dir().unwrap().join(name);
|
||||
|
||||
fs::create_dir_all(&repo).unwrap();
|
||||
repo::clone(&repo, &url).expect("Failed to clone repository");
|
||||
}
|
||||
"push" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
repo::push(&dir).expect("Failed to push repository");
|
||||
} else {
|
||||
eprintln!("No Dsx.toml found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
"pull" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
repo::pull(&dir).expect("Failed to pull repository");
|
||||
} else {
|
||||
eprintln!("No Dsx.toml found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
"package" => todo!("Package manager stub – not implemented yet."),
|
||||
_ => {
|
||||
eprintln!("Unknown command: {}", args[1]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_project_root() -> Option<PathBuf> {
|
||||
// check if current dir has Dsx.toml otherwise check parent dir recursively
|
||||
let mut cwd = env::current_dir().unwrap();
|
||||
loop {
|
||||
let dsx_toml = cwd.join("Dsx.toml");
|
||||
if dsx_toml.exists() {
|
||||
return Some(cwd);
|
||||
}
|
||||
|
||||
if let Some(parent) = cwd.parent() {
|
||||
cwd = parent.to_path_buf();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
use std::{env, fmt, fs, path::PathBuf};
|
||||
use std::{env, fmt, fs};
|
||||
|
||||
use dsx::common::{
|
||||
config::DsxConfig,
|
||||
templates::{self, Dsa, Dsc, Template},
|
||||
};
|
||||
use dsx_common::config::DsxConfig;
|
||||
|
||||
use crate::templates::{self, Dsa, Dsc, Template};
|
||||
|
||||
// ---------- new project ----------------------------------------------------
|
||||
pub fn new_project(args: &[String]) {
|
||||
@@ -38,13 +37,18 @@ pub fn new_project(args: &[String]) {
|
||||
|
||||
fs::create_dir_all(src_path.join("lib")).expect("Failed to create lib directory");
|
||||
fs::write(
|
||||
src_path.join("lib/print.dsa"),
|
||||
templates::create_print_lib(),
|
||||
src_path.join("./lib/print.dsa"),
|
||||
include_str!("../templates/dsa/print.dsa"),
|
||||
)
|
||||
.expect("Failed to create print.dsa");
|
||||
fs::write(
|
||||
src_path.join("lib/maths.dsa"),
|
||||
templates::create_maths_lib(),
|
||||
src_path.join("./lib/maths.dsa"),
|
||||
include_str!("../templates/dsa/maths.dsa"),
|
||||
)
|
||||
.expect("Failed to create maths.dsa");
|
||||
fs::write(
|
||||
src_path.join("./lib/serial.dsa"),
|
||||
include_str!("../templates/dsa/serial.dsa"),
|
||||
)
|
||||
.expect("Failed to create maths.dsa");
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
use dsx_common::config::DsxConfig;
|
||||
use flate2::read::GzDecoder;
|
||||
use flate2::{Compression, write::GzEncoder};
|
||||
use reqwest::Url;
|
||||
use std::fs::{self, File};
|
||||
use std::path::Path;
|
||||
use tar::Builder;
|
||||
|
||||
pub fn push(repo_dir: &Path) -> Result<(), DsxError> {
|
||||
let config_file = fs::read_to_string(repo_dir.join("Dsx.toml"))?;
|
||||
let config: DsxConfig = toml::from_str(&config_file).expect("Failed to parse config");
|
||||
|
||||
let mut repo_url =
|
||||
Url::parse(&config.remote_url.expect(
|
||||
"Repository URL is not set in Dsx.toml, set it with the key 'remote'",
|
||||
))
|
||||
.unwrap();
|
||||
repo_url.path_segments_mut().unwrap().push("push");
|
||||
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client
|
||||
.post(repo_url)
|
||||
.body(pack_tarball(repo_dir)?)
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.send()
|
||||
.map_err(|e| {
|
||||
DsxError::with_context(
|
||||
format!("failed to stream to client: {}", e),
|
||||
ErrorType::IoError,
|
||||
)
|
||||
})?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DsxError::with_context(
|
||||
response.text().unwrap(),
|
||||
ErrorType::IoError,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pull(repo_dir: &Path) -> Result<(), DsxError> {
|
||||
let config_file = fs::read_to_string(repo_dir.join("Dsx.toml"))?;
|
||||
let config: DsxConfig = toml::from_str(&config_file).expect("Failed to parse config");
|
||||
|
||||
let repo_url =
|
||||
Url::parse(&config.remote_url.expect(
|
||||
"Repository URL is not set in Dsx.toml, set it with the key 'remote'",
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
clone(repo_dir, &repo_url)
|
||||
}
|
||||
|
||||
pub fn clone(repo_dir: &Path, url: &Url) -> Result<(), DsxError> {
|
||||
let mut url = url.clone();
|
||||
url.path_segments_mut().unwrap().push("pull");
|
||||
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client.get(url).send().map_err(|e| {
|
||||
DsxError::with_context(
|
||||
format!("failed to stream from client: {e}"),
|
||||
ErrorType::IoError,
|
||||
)
|
||||
})?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(DsxError::with_context(
|
||||
response.text().unwrap(),
|
||||
ErrorType::IoError,
|
||||
));
|
||||
}
|
||||
|
||||
let tmp_file = std::env::temp_dir().join("dsx-pull.tar.gz");
|
||||
std::fs::write(&tmp_file, response.bytes().unwrap())
|
||||
.map_err(|e| DsxError::with_context(e.to_string(), ErrorType::IoError))?;
|
||||
unpack_tarball(&tmp_file, repo_dir)?;
|
||||
fs::remove_file(tmp_file)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unpack_tarball(
|
||||
archive: &std::path::Path,
|
||||
dest: &std::path::Path,
|
||||
) -> Result<(), DsxError> {
|
||||
let file = File::open(archive)
|
||||
.map_err(|e| DsxError::with_context(e.to_string(), ErrorType::IoError))?;
|
||||
|
||||
fs::create_dir_all(dest)?;
|
||||
|
||||
let gz = GzDecoder::new(file);
|
||||
let mut tar = tar::Archive::new(gz);
|
||||
tar.unpack(dest)
|
||||
.map_err(|e| DsxError::with_context(e.to_string(), ErrorType::TarError))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_tarball(src_dir: &std::path::Path) -> Result<Vec<u8>, DsxError> {
|
||||
let buf = Vec::new();
|
||||
let gz = GzEncoder::new(buf, Compression::default());
|
||||
let mut tar = Builder::new(gz);
|
||||
tar.append_dir_all(".", src_dir)?;
|
||||
let gz = tar.into_inner()?;
|
||||
Ok(gz.finish()?)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DsxError {
|
||||
pub message: String,
|
||||
pub r#type: ErrorType,
|
||||
}
|
||||
|
||||
impl DsxError {
|
||||
pub fn new(message: impl AsRef<str>) -> Self {
|
||||
DsxError {
|
||||
message: message.as_ref().to_string(),
|
||||
r#type: ErrorType::Generic,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_context(message: impl AsRef<str>, r#type: ErrorType) -> Self {
|
||||
DsxError {
|
||||
message: message.as_ref().to_string(),
|
||||
r#type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for DsxError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Self::with_context(err.to_string(), ErrorType::IoError)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub enum ErrorType {
|
||||
BuildFailed,
|
||||
IoError,
|
||||
|
||||
#[default]
|
||||
Generic,
|
||||
TarError,
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub trait Template {
|
||||
fn lib(project: &str) -> String;
|
||||
fn bin(project: &str) -> String;
|
||||
|
||||
fn create(project: &str, lib: bool) -> String {
|
||||
if lib {
|
||||
Self::lib(project)
|
||||
} else {
|
||||
Self::bin(project)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Dsa;
|
||||
pub struct Dsc;
|
||||
|
||||
impl Template for Dsa {
|
||||
fn lib(project: &str) -> String {
|
||||
format!(
|
||||
r#"//
|
||||
lib.dsa
|
||||
// usage:
|
||||
//
|
||||
// include {project} "<relative path>"
|
||||
//
|
||||
// usage for {project}_main:
|
||||
// push (arg1)
|
||||
// push (arg0)
|
||||
// call {project}::{project}_main
|
||||
// pop (arg0)
|
||||
// pop (arg1)
|
||||
|
||||
// Example data declarations
|
||||
// dw example_data: 0x0000
|
||||
|
||||
// Main function template
|
||||
{project}_main:
|
||||
// the correct way to start a function as defined by the calling convention
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
// explanation of how to access args
|
||||
ldw bpr, rg0, 8 // arg 0
|
||||
ldw bpr, rg0, 12 // arg 1
|
||||
|
||||
// your code goes here
|
||||
// Example: load example_data into rg1
|
||||
// ldw example_data, rg1
|
||||
|
||||
// the correct way to end a function as defined by the calling convention
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
fn bin(project: &str) -> String {
|
||||
format!(
|
||||
r#"
|
||||
// GENERATED BY DSX-BUILD
|
||||
// Generated at: {timestamp}
|
||||
// Project name: {project}
|
||||
|
||||
// Imports
|
||||
include print: "./lib/print.dsa"
|
||||
|
||||
// Globals & Reserved Memory
|
||||
dw stack: 0x10000
|
||||
db message: "Process Exited with code:"
|
||||
|
||||
// Entry Point
|
||||
_init:
|
||||
ldw stack, bpr
|
||||
mov bpr, spr
|
||||
push zero
|
||||
call main
|
||||
call print::print_newline
|
||||
lwi message, rg0
|
||||
push rg0
|
||||
call print::print
|
||||
pop zero
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
hlt
|
||||
|
||||
main:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
// Your code goes here
|
||||
|
||||
// Return zero
|
||||
stw zero, bpr, 8
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return"#,
|
||||
timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Template for Dsc {
|
||||
fn lib(project: &str) -> String {
|
||||
format!(
|
||||
r#"
|
||||
// GENERATED BY DSX-BUILD
|
||||
// Generated at: {timestamp}
|
||||
// Project name: {project}
|
||||
|
||||
// Imports
|
||||
include print: "./lib/print.dsa";
|
||||
|
||||
// Main Function
|
||||
fn {project}_main() -> u32 {{
|
||||
return 0;
|
||||
}}"#,
|
||||
timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S")
|
||||
)
|
||||
}
|
||||
|
||||
fn bin(project: &str) -> String {
|
||||
format!(
|
||||
r#"
|
||||
// GENERATED BY DSX-BUILD
|
||||
// Generated at: {timestamp}
|
||||
// Project name: {project}
|
||||
|
||||
// Imports
|
||||
include print: "./lib/print.dsa";
|
||||
|
||||
// Main Function
|
||||
fn main() -> u32 {{
|
||||
return 0;
|
||||
}}"#,
|
||||
timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S")
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
// multiply.dsa
|
||||
// usage:
|
||||
//
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
// lib:
|
||||
// print.dsa
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
// lib:
|
||||
// print_serial.dsa
|
||||
|
||||
// usage:
|
||||
//
|
||||
// include print_serial "<relative path>"
|
||||
//
|
||||
// usage for print:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print_serial::print
|
||||
//
|
||||
// usage for print_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print_serial::print_byte
|
||||
//
|
||||
// usage for print_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print_serial::print_word
|
||||
//
|
||||
// usage for print_hex_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print_serial::print_hex_byte
|
||||
//
|
||||
// usage for print_hex_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print_serial::print_hex_word
|
||||
//
|
||||
// usage for print_num:
|
||||
// push (register containing number to print in decimal)
|
||||
// push pcx
|
||||
// jmp print_serial::print_num
|
||||
//
|
||||
// usage for println:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print_serial::println
|
||||
//
|
||||
|
||||
include maths "../maths.dsa"
|
||||
|
||||
dw serial: 0x207D0 // 0x20000 + 2000
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) to the serial port.
|
||||
print:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_print_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
jmp _print_loop
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) followed by a newline to the serial port.
|
||||
println:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_println_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _println_end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
jmp _println_loop
|
||||
|
||||
_println_end:
|
||||
lli 0x0A, rg2 // newline character
|
||||
stb rg2, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the word in arg[0] as 4 raw bytes to the serial port.
|
||||
print_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the serial port.
|
||||
print_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the value of arg[0] to the serial port in hex.
|
||||
print_hex_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
ldb bpr, rg0, 8
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 9
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 10
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 11
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the serial port in hex.
|
||||
print_hex_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
call _print_hex_byte
|
||||
jmp _end
|
||||
|
||||
// function body
|
||||
_print_hex_byte:
|
||||
lli 0xF, rg2
|
||||
push rg0
|
||||
|
||||
shr rg0, 4
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
pop rg0
|
||||
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
return
|
||||
|
||||
// print a hex digit
|
||||
_print_hex_nibble:
|
||||
lli 10, rg3
|
||||
cmp rg0, rg3
|
||||
jlt _print_hex_nibble_number
|
||||
addi rg0, 0x37, rg0
|
||||
stb rg0, rg1
|
||||
return
|
||||
|
||||
_print_hex_nibble_number:
|
||||
addi rg0, 0x30, rg0
|
||||
stb rg0, rg1
|
||||
return
|
||||
|
||||
// ------------------------------------------
|
||||
// prints arg[0] as a decimal number to the serial port.
|
||||
print_num:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lli 0, rg5
|
||||
|
||||
cmp rg0, zero
|
||||
jne _print_num_extract_digits
|
||||
|
||||
lli 0x30, rg6
|
||||
push rg6
|
||||
lli 1, rg5
|
||||
jmp _print_num_output
|
||||
|
||||
_print_num_extract_digits:
|
||||
cmp rg0, zero
|
||||
jeq _print_num_output
|
||||
|
||||
push rg0
|
||||
lli 10, rg1
|
||||
push rg1
|
||||
call maths::divmod
|
||||
pop rg0
|
||||
pop rg1
|
||||
|
||||
addi rg1, 0x30, rg6
|
||||
push rg6
|
||||
inc rg5
|
||||
|
||||
jmp _print_num_extract_digits
|
||||
|
||||
_print_num_output:
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_print_num_output_loop:
|
||||
cmp rg5, zero
|
||||
jeq _print_num_done
|
||||
|
||||
pop rg6
|
||||
stb rg6, rg1
|
||||
dec rg5
|
||||
|
||||
jmp _print_num_output_loop
|
||||
|
||||
_print_num_done:
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// return
|
||||
_end:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "dsx_common"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
compiler = { path = "../../core/compiler" }
|
||||
assembler = { path = "../../core/assembler" }
|
||||
common = { path = "../../core/dsa_common" }
|
||||
|
||||
chrono = "0.4.44"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
toml = "1.0.3"
|
||||
walkdir = "2.5.0"
|
||||
@@ -4,20 +4,32 @@ use std::{
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use crate::common::config::DsxConfig;
|
||||
use crate::config::DsxConfig;
|
||||
|
||||
use assembler::prelude::Assembler;
|
||||
use common::build::{BuildError, Builder};
|
||||
use compiler::Compiler;
|
||||
|
||||
pub struct BuildContext {
|
||||
pub project_dir: PathBuf,
|
||||
pub build_dir: PathBuf,
|
||||
pub artifact_dir: PathBuf,
|
||||
}
|
||||
|
||||
// ---------- build ----------------------------------------------------------
|
||||
pub fn build_project(cwd: &Path) -> Result<(), BuildError> {
|
||||
let config: DsxConfig = toml::from_str(&fs::read_to_string(cwd.join("Dsx.toml"))?)
|
||||
.map_err(|deser_err| {
|
||||
pub fn build_project(ctx: BuildContext) -> Result<(), BuildError> {
|
||||
// variables
|
||||
let binary_path = ctx.artifact_dir.join("out.dsb");
|
||||
let main_path = ctx.build_dir.join("main.dsa");
|
||||
let config_path = ctx.project_dir.join("Dsx.toml");
|
||||
|
||||
let src_dir = ctx.project_dir.join("src");
|
||||
|
||||
let config: DsxConfig =
|
||||
toml::from_str(&fs::read_to_string(&config_path)?).map_err(|deser_err| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, deser_err.to_string())
|
||||
})?;
|
||||
|
||||
let src_dir = cwd.join("src");
|
||||
if !src_dir.exists() {
|
||||
return Err(BuildError::Generic(String::from(
|
||||
"Source Directory does not exist",
|
||||
@@ -35,36 +47,38 @@ pub fn build_project(cwd: &Path) -> Result<(), BuildError> {
|
||||
let (has_dsa, has_dsc) = detect_source_language(&src_dir);
|
||||
|
||||
// create a build dir and copy all files across
|
||||
let build_dir = cwd.join("build");
|
||||
fs::create_dir_all(&build_dir)?;
|
||||
env::set_current_dir(&build_dir)?;
|
||||
|
||||
copy_recursively(&src_dir, &build_dir)?;
|
||||
fs::create_dir_all(&ctx.build_dir)?;
|
||||
copy_recursively(&src_dir, &ctx.build_dir)?;
|
||||
|
||||
if has_dsc {
|
||||
build_all_dsc(&build_dir)?;
|
||||
build_all_dsc(&ctx.build_dir)?;
|
||||
}
|
||||
|
||||
// Replace .dsc with .dsa only in include statements, recursively for each file.
|
||||
let mut sed_cmd = Command::new("bash");
|
||||
sed_cmd.args([
|
||||
|
||||
let status = Command::new("bash").args([
|
||||
"-c",
|
||||
&format!(
|
||||
"find \"{}\" -type f -name '*.dsa' -exec sed -i '/^include/ s/\\.dsc/.dsa/g' {{}} +",
|
||||
build_dir.display()
|
||||
ctx.build_dir.display()
|
||||
),
|
||||
]);
|
||||
run(&mut sed_cmd);
|
||||
]).status()?;
|
||||
|
||||
if !status.success() {
|
||||
return Err(BuildError::IoError(String::from(
|
||||
"Failed to execute build command command",
|
||||
)));
|
||||
}
|
||||
|
||||
// assemble result
|
||||
{
|
||||
fs::create_dir_all(cwd.join("artifacts"))?;
|
||||
let mut asm = Assembler::new("./main.dsa");
|
||||
asm.start();
|
||||
asm.write_result("../artifacts/out.dsb")?;
|
||||
fs::create_dir_all(&ctx.artifact_dir)?;
|
||||
let mut asm = Assembler::new(&main_path);
|
||||
asm.start(());
|
||||
asm.write_result(&binary_path)?;
|
||||
}
|
||||
|
||||
println!("Build finished. Binary at {}/main.dsb", build_dir.display());
|
||||
println!("Build finished. Binary at {}", binary_path.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -143,18 +157,12 @@ fn build_all_dsc(path: &Path) -> Result<(), BuildError> {
|
||||
let input_path = path;
|
||||
let output_path = path.with_extension("dsa");
|
||||
|
||||
let is_lib = !(input_path.file_stem().unwrap().to_str().unwrap() == "main");
|
||||
|
||||
let mut compiler = Compiler::new(input_path);
|
||||
compiler.start();
|
||||
compiler.start(is_lib);
|
||||
compiler.write_result(output_path.clone())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Run a command and exit on failure.
|
||||
fn run(cmd: &mut Command) {
|
||||
let status = cmd.status().expect("failed to execute command");
|
||||
if !status.success() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
@@ -140,7 +140,7 @@ fn main() -> u32 {{
|
||||
}
|
||||
|
||||
pub fn create_print_lib() -> String {
|
||||
format!(
|
||||
String::from(
|
||||
r#"
|
||||
// lib:
|
||||
// print.dsa
|
||||
@@ -473,12 +473,12 @@ _end:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
"#
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_maths_lib() -> String {
|
||||
format!(
|
||||
String::from(
|
||||
r#"
|
||||
// multiply.dsa
|
||||
// usage:
|
||||
@@ -584,6 +584,6 @@ skip_add:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
"#
|
||||
"#,
|
||||
)
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
use rocket::serde;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -9,6 +8,7 @@ pub struct DsxConfig {
|
||||
pub description: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde(rename = "remote")]
|
||||
pub remote_url: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
@@ -1,3 +1,2 @@
|
||||
pub mod builder;
|
||||
pub mod config;
|
||||
pub mod templates;
|
||||
@@ -0,0 +1,4 @@
|
||||
id = "test"
|
||||
latest_build_date = "2026-02-25 14:39:49"
|
||||
latest_build_status = "success"
|
||||
latest_build_id = "test"
|
||||
@@ -0,0 +1,3 @@
|
||||
name = "test"
|
||||
binaries = []
|
||||
remote = "http://localhost:8000/api/pkg/test"
|
||||
@@ -0,0 +1,77 @@
|
||||
// Arena Allocator
|
||||
// Supports multiple arenas that can be destroyed independently
|
||||
// Much more practical than a simple bump allocator
|
||||
|
||||
// Global heap management
|
||||
static heap_start: u32 = 0x30000;
|
||||
static heap_end: u32 = 0x40000;
|
||||
static heap_current: u32 = 0x30000;
|
||||
|
||||
// Arena structure (stored at the start of each arena):
|
||||
// [0-3]: start_address (u32)
|
||||
// [4-7]: current_position (u32)
|
||||
// [8-11]: end_address (u32)
|
||||
// Total header size: 12 bytes
|
||||
|
||||
// Create a new arena with given size
|
||||
// Returns pointer to arena handle (or 0 if failed)
|
||||
fn new(size: u32) -> u32 {
|
||||
let total_size: u32 = size + 12;
|
||||
let arena_ptr: u32 = heap_current;
|
||||
let new_current: u32 = arena_ptr + total_size;
|
||||
|
||||
// Check if we have space
|
||||
if new_current > heap_end {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate arena data region
|
||||
let data_start: u32 = arena_ptr + 12;
|
||||
let data_end: u32 = arena_ptr + total_size;
|
||||
|
||||
// Initialize arena header
|
||||
// Note: In real implementation, you'd use pointer writes here
|
||||
// For now, using placeholder comments:
|
||||
*arena_ptr = data_start; // start_address
|
||||
*(arena_ptr + 4) = data_start; // current_position
|
||||
*(arena_ptr + 8) = data_end; // end_address
|
||||
|
||||
heap_current = new_current;
|
||||
|
||||
return arena_ptr;
|
||||
}
|
||||
|
||||
// Allocate from an arena
|
||||
// Returns pointer to allocated memory (or 0 if failed)
|
||||
fn alloc(arena: u32, size: u32) -> u32 {
|
||||
// Read current position from arena
|
||||
let current: u32 = *(arena + 4);
|
||||
let end: u32 = *(arena + 8);
|
||||
|
||||
let new_current: u32 = current + size;
|
||||
|
||||
// Check if arena has space
|
||||
if new_current > end {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Update current position in arena
|
||||
*(arena + 4) = new_current;
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
// Destroy an arena (in bump allocator, this is a no-op)
|
||||
// In a real allocator, you'd mark the memory as free
|
||||
fn destroy(arena: u32) {
|
||||
// In a true allocator, mark memory as reusable
|
||||
// For bump allocator, we can't reclaim memory
|
||||
// unless we destroy ALL arenas and reset
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reset entire heap (destroys ALL arenas)
|
||||
fn reset_all() {
|
||||
heap_current = heap_start;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// multiply.dsa
|
||||
// usage:
|
||||
//
|
||||
// include multiply "<relative path>"
|
||||
//
|
||||
// usage for multiply:
|
||||
// push (arg1)
|
||||
// push (arg0)
|
||||
// call multiply::multiply
|
||||
// pop (arg0)
|
||||
// pop (arg1)
|
||||
|
||||
multiply:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8 // load op 2
|
||||
ldw bpr, rg1, 12 // load op 1
|
||||
lwi 0, rg2 // initialise rg2 to zero
|
||||
|
||||
_multiply_loop:
|
||||
add rg2, rg0, rg2
|
||||
dec rg1
|
||||
|
||||
cmp rg1, zero
|
||||
jgt _multiply_loop
|
||||
|
||||
_multiply_end:
|
||||
stw rg2, bpr, 8
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
|
||||
divmod:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg1, 8 // load op 2
|
||||
ldw bpr, rg0, 12 // load op 1
|
||||
|
||||
lli 0, rg3
|
||||
|
||||
_divmod_loop:
|
||||
cmp rg0, rg1
|
||||
jlt _divmod_end
|
||||
|
||||
sub rg0, rg1, rg0
|
||||
inc rg3
|
||||
|
||||
jmp _divmod_loop
|
||||
|
||||
_divmod_end:
|
||||
// store div in first arg
|
||||
// store mod in second arg
|
||||
stw rg3, bpr, 8
|
||||
stw rg0, bpr, 12
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
|
||||
// multiply.dsa - improved version
|
||||
// Multiplies two 32-bit numbers using shift-and-add
|
||||
//
|
||||
// Usage:
|
||||
// push operand2 (multiplier)
|
||||
// push operand1 (multiplicand)
|
||||
// call multiply::multiply
|
||||
// pop result
|
||||
// pop zero (discard second argument)
|
||||
|
||||
new_multiply:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8 // rg0 = multiplicand
|
||||
ldw bpr, rg1, 12 // rg1 = multiplier
|
||||
|
||||
lli 0, rg2 // rg2 = result (accumulator)
|
||||
lli 32, rg3 // rg3 = bit counter
|
||||
|
||||
mult_loop:
|
||||
// Check if lowest bit of multiplier is 1
|
||||
lli 1, acc
|
||||
and rg1, acc, acc // acc = rg1 & 1
|
||||
cmp acc, zero
|
||||
jeq skip_add // if (rg1 & 1) == 0, skip addition
|
||||
|
||||
// Add multiplicand to result
|
||||
add rg2, rg0, rg2
|
||||
|
||||
skip_add:
|
||||
shl rg0, 1 // shift multiplicand left
|
||||
shr rg1, 1 // shift multiplier right
|
||||
|
||||
dec rg3
|
||||
cmp rg3, zero
|
||||
jgt mult_loop
|
||||
|
||||
stw rg2, bpr, 8 // store result
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
@@ -0,0 +1,331 @@
|
||||
// lib:
|
||||
// print.dsa
|
||||
|
||||
// usage:
|
||||
//
|
||||
// include print "<relative path>""
|
||||
//
|
||||
// usage for print:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print::print
|
||||
//
|
||||
// usage for reset:
|
||||
// push pcx
|
||||
// jmp print::reset
|
||||
//
|
||||
// usage for clear:
|
||||
// push pcx
|
||||
// jmp print::clear
|
||||
//
|
||||
// usage for print_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print::print_byte
|
||||
//
|
||||
// usage for print_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print::print_word
|
||||
//
|
||||
// usage for print_num:
|
||||
// push (register containing number to print in decimal)
|
||||
// push pcx
|
||||
// jmp print::print_num
|
||||
//
|
||||
|
||||
include maths "./maths.dsa"
|
||||
|
||||
dw display: 0x20000
|
||||
dw current: 0x20000
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) to the screen. (no trailing whitespace unless explicitly provided)
|
||||
print:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
ldw current, rg1
|
||||
|
||||
_print_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
addi rg1, 1
|
||||
|
||||
jmp _print_loop
|
||||
|
||||
// ------------------------------------------
|
||||
println:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
ldw current, rg1
|
||||
|
||||
_println_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _println_end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
addi rg1, 1
|
||||
|
||||
jmp _println_loop
|
||||
|
||||
_println_end:
|
||||
call print_newline
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the value of arg[0] to the screen.
|
||||
print_word:
|
||||
// initialise
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
// load byte into acc
|
||||
ldw bpr, rg0, 8
|
||||
ldw current, rg1
|
||||
|
||||
addi rg1, 3
|
||||
|
||||
stb rg0, rg1
|
||||
subi rg1, 1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
subi rg1, 1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
subi rg1, 1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
|
||||
addi rg1, 4
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the screen.
|
||||
print_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
ldw current, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
addi rg1, 1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the value of arg[0] to the screen in hex.
|
||||
print_hex_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw current, rg1
|
||||
|
||||
ldb bpr, rg0, 8
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 9
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 10
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 11
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the screen in hex.
|
||||
print_hex_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
ldw current, rg1
|
||||
|
||||
call _print_hex_byte
|
||||
jmp _end
|
||||
|
||||
// function body
|
||||
_print_hex_byte:
|
||||
// mask to get lower nibble
|
||||
lli 0xF, rg2
|
||||
// save rg0 state
|
||||
push rg0
|
||||
|
||||
shr rg0, 4
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
pop rg0
|
||||
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
return
|
||||
|
||||
// print a hex digit
|
||||
_print_hex_nibble:
|
||||
lli 10, rg3
|
||||
cmp rg0, rg3
|
||||
jlt _print_hex_nibble_number
|
||||
addi rg0, 0x37, rg0
|
||||
stb rg0, rg1
|
||||
addi rg1, 1
|
||||
return
|
||||
|
||||
// helper function.
|
||||
_print_hex_nibble_number:
|
||||
addi rg0, 0x30, rg0
|
||||
stb rg0, rg1
|
||||
addi rg1, 1
|
||||
return
|
||||
|
||||
// ------------------------------------------
|
||||
// print whitespace
|
||||
print_whitespace:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw current, rg1
|
||||
lli 0x20, rg0
|
||||
stb rg0, rg1
|
||||
addi rg1, 1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// print newline
|
||||
print_newline:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
// load variables into registers
|
||||
ldw display, rg0
|
||||
ldw current, rg1
|
||||
|
||||
// get the offset from the display base
|
||||
sub rg1, rg0, rg0
|
||||
|
||||
lwi 80, rg2
|
||||
pusha 3
|
||||
push rg0
|
||||
push rg2
|
||||
call maths::divmod
|
||||
pop zero // result
|
||||
pop rg3 // remainder
|
||||
popa 3
|
||||
|
||||
sub rg1, rg3, rg2
|
||||
addi rg2, 80, rg1
|
||||
|
||||
// _end saves the display state
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints arg[0] as a decimal number to the screen.
|
||||
print_num:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8 // load number to print
|
||||
lli 0, rg5 // rg5 = digit counter
|
||||
|
||||
// check if number is zero
|
||||
cmp rg0, zero
|
||||
jne _print_num_extract_digits
|
||||
|
||||
// special case: print '0' for zero
|
||||
lli 0x30, rg6
|
||||
push rg6 // push digit to stack buffer
|
||||
lli 1, rg5 // we have 1 digit
|
||||
jmp _print_num_output
|
||||
|
||||
_print_num_extract_digits:
|
||||
// divide by 10 repeatedly to get digits
|
||||
cmp rg0, zero
|
||||
jeq _print_num_output
|
||||
|
||||
// call divmod(rg0, 10)
|
||||
push rg0 // dividend
|
||||
lli 10, rg1
|
||||
push rg1 // divisor (10)
|
||||
call maths::divmod
|
||||
pop rg0 // quotient (continue dividing this)
|
||||
pop rg1 // remainder (the digit)
|
||||
|
||||
// convert digit to ASCII and push to stack buffer
|
||||
addi rg1, 0x30, rg6 // convert to ASCII
|
||||
push rg6 // push digit to stack
|
||||
inc rg5 // increment digit counter
|
||||
|
||||
jmp _print_num_extract_digits
|
||||
|
||||
_print_num_output:
|
||||
// now print digits (pop them off in reverse order)
|
||||
ldw current, rg1 // get display pointer
|
||||
|
||||
_print_num_output_loop:
|
||||
// check if we've printed all digits
|
||||
cmp rg5, zero
|
||||
jeq _print_num_done
|
||||
|
||||
// pop digit and print it
|
||||
pop rg6
|
||||
stb rg6, rg1
|
||||
addi rg1, 1
|
||||
dec rg5
|
||||
|
||||
jmp _print_num_output_loop
|
||||
|
||||
_print_num_done:
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// resets the cursor position on the screen to 0x20000. (0,0)
|
||||
reset:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
ldw display, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// clears the screen
|
||||
clear:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
// display size = 2000 bytes / 500 words
|
||||
lli 500 rg0
|
||||
ldw display, rg1
|
||||
|
||||
_clear_loop:
|
||||
dec rg0
|
||||
stw zero, rg1
|
||||
addi rg1, 4
|
||||
cmp rg0, zero
|
||||
jgt _clear_loop
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// return
|
||||
_end:
|
||||
stw rg1, current
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
@@ -0,0 +1,274 @@
|
||||
// lib:
|
||||
// print_serial.dsa
|
||||
|
||||
// usage:
|
||||
//
|
||||
// include print_serial "<relative path>"
|
||||
//
|
||||
// usage for print:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print_serial::print
|
||||
//
|
||||
// usage for print_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print_serial::print_byte
|
||||
//
|
||||
// usage for print_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print_serial::print_word
|
||||
//
|
||||
// usage for print_hex_byte:
|
||||
// push (register containing byte)
|
||||
// push pcx
|
||||
// jmp print_serial::print_hex_byte
|
||||
//
|
||||
// usage for print_hex_word:
|
||||
// push (register containing word)
|
||||
// push pcx
|
||||
// jmp print_serial::print_hex_word
|
||||
//
|
||||
// usage for print_whitespace:
|
||||
// push pcx
|
||||
// jmp print_serial::print_whitespace
|
||||
//
|
||||
// usage for print_newline:
|
||||
// push pcx
|
||||
// jmp print_serial::print_newline
|
||||
//
|
||||
// usage for print_num:
|
||||
// push (register containing number to print in decimal)
|
||||
// push pcx
|
||||
// jmp print_serial::print_num
|
||||
//
|
||||
// usage for println:
|
||||
// push (register containing address of string)
|
||||
// push pcx
|
||||
// jmp print_serial::println
|
||||
//
|
||||
|
||||
include maths "./maths.dsa"
|
||||
|
||||
dw serial: 0x207D0 // 0x20000 + 2000
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) to the serial port.
|
||||
print:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_print_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
jmp _print_loop
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the string at addr(arg[0]) followed by a newline to the serial port.
|
||||
println:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_println_loop:
|
||||
ldb rg0, acc
|
||||
cmp acc, zero
|
||||
jeq _println_end
|
||||
stb acc, rg1
|
||||
|
||||
addi rg0, 1
|
||||
jmp _println_loop
|
||||
|
||||
_println_end:
|
||||
lli 0x0A, rg2 // newline character
|
||||
stb rg2, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the word in arg[0] as 4 raw bytes to the serial port.
|
||||
print_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
shr rg0, 8
|
||||
stb rg0, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the serial port.
|
||||
print_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
stb rg0, rg1
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the value of arg[0] to the serial port in hex.
|
||||
print_hex_word:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
ldb bpr, rg0, 8
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 9
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 10
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
ldb bpr, rg0, 11
|
||||
push rg0
|
||||
call _print_hex_byte
|
||||
addi spr, 4
|
||||
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints the last byte of arg[0] to the serial port in hex.
|
||||
print_hex_byte:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
call _print_hex_byte
|
||||
jmp _end
|
||||
|
||||
// function body
|
||||
_print_hex_byte:
|
||||
lli 0xF, rg2
|
||||
push rg0
|
||||
|
||||
shr rg0, 4
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
pop rg0
|
||||
|
||||
and rg0, rg2, rg0
|
||||
call _print_hex_nibble
|
||||
return
|
||||
|
||||
// print a hex digit
|
||||
_print_hex_nibble:
|
||||
lli 10, rg3
|
||||
cmp rg0, rg3
|
||||
jlt _print_hex_nibble_number
|
||||
addi rg0, 0x37, rg0
|
||||
stb rg0, rg1
|
||||
return
|
||||
|
||||
_print_hex_nibble_number:
|
||||
addi rg0, 0x30, rg0
|
||||
stb rg0, rg1
|
||||
return
|
||||
|
||||
// print a single space
|
||||
print_whitespace:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lli 0x20, rg0
|
||||
ldw serial, rg1
|
||||
stb rg0, rg1
|
||||
|
||||
jmp _end
|
||||
|
||||
// print a single space
|
||||
print_newline:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
lli 0x0A, rg0
|
||||
ldw serial, rg1
|
||||
stb rg0, rg1
|
||||
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// prints arg[0] as a decimal number to the serial port.
|
||||
print_num:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
ldw bpr, rg0, 8
|
||||
lli 0, rg5
|
||||
|
||||
cmp rg0, zero
|
||||
jne _print_num_extract_digits
|
||||
|
||||
lli 0x30, rg6
|
||||
push rg6
|
||||
lli 1, rg5
|
||||
jmp _print_num_output
|
||||
|
||||
_print_num_extract_digits:
|
||||
cmp rg0, zero
|
||||
jeq _print_num_output
|
||||
|
||||
push rg0
|
||||
lli 10, rg1
|
||||
push rg1
|
||||
call maths::divmod
|
||||
pop rg0
|
||||
pop rg1
|
||||
|
||||
addi rg1, 0x30, rg6
|
||||
push rg6
|
||||
inc rg5
|
||||
|
||||
jmp _print_num_extract_digits
|
||||
|
||||
_print_num_output:
|
||||
lwi 0x207D0, rg1
|
||||
|
||||
_print_num_output_loop:
|
||||
cmp rg5, zero
|
||||
jeq _print_num_done
|
||||
|
||||
pop rg6
|
||||
stb rg6, rg1
|
||||
dec rg5
|
||||
|
||||
jmp _print_num_output_loop
|
||||
|
||||
_print_num_done:
|
||||
jmp _end
|
||||
|
||||
// ------------------------------------------
|
||||
// return
|
||||
_end:
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
@@ -0,0 +1,30 @@
|
||||
include serial: "./lib/serial.dsa";
|
||||
include print: "./lib/print.dsa";
|
||||
include arena: "./lib/arena_alloc.dsc";
|
||||
|
||||
fn main() {
|
||||
let x: u32 = 0;
|
||||
let y: u32 = &x;
|
||||
|
||||
let alloc: u32 = arena::new(512);
|
||||
let ptr1: u32 = arena::alloc(alloc, 32);
|
||||
let ptr2: u32 = arena::alloc(alloc, 32);
|
||||
|
||||
serial::print_hex_word(alloc);
|
||||
serial::print_newline();
|
||||
serial::print_hex_word(ptr1);
|
||||
serial::print_newline();
|
||||
serial::print_hex_word(ptr2);
|
||||
serial::print_newline();
|
||||
serial::print_num(*ptr2);
|
||||
serial::print_newline();
|
||||
*ptr2 = 42;
|
||||
|
||||
serial::print_hex_word(ptr2);
|
||||
serial::print_whitespace();
|
||||
serial::print_num(*ptr2);
|
||||
serial::print_newline();
|
||||
serial::println("end");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
[package]
|
||||
name = "dsx"
|
||||
name = "dsx_server"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "dsx_server"
|
||||
path = "src/server/server.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "dsx"
|
||||
path = "src/client/client.rs"
|
||||
name = "dsx-server"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
compiler = { path = "../compiler" }
|
||||
assembler = { path = "../assembler" }
|
||||
common = { path = "../common" }
|
||||
compiler = { path = "../../core/compiler" }
|
||||
assembler = { path = "../../core/assembler" }
|
||||
common = { path = "../../core/dsa_common" }
|
||||
dsx_common = { path = "../dsx_common" }
|
||||
|
||||
anyhow = "1.0.102"
|
||||
dotenv = "0.15.0"
|
||||
rocket = { version = "0.5.1", features = ["json"] }
|
||||
rocket_dyn_templates = { version = "0.2.0", features = ["tera"] }
|
||||
@@ -29,3 +25,4 @@ chrono = "0.4.43"
|
||||
tar = "0.4.44"
|
||||
flate2 = "1.1.9"
|
||||
walkdir = "2.5.0"
|
||||
tokio = { version = "1.49.0", features = ["rt"] }
|
||||
@@ -59,7 +59,7 @@ on_success = "back" # so that we don't open the browser at each change
|
||||
# if it makes sense for this crate.
|
||||
[jobs.run]
|
||||
command = [
|
||||
"cargo", "run", "--bin", "dsx_server"
|
||||
"cargo", "run", "--bin", "dsx-server"
|
||||
# put launch parameters for your program behind a `--` separator
|
||||
]
|
||||
need_stdout = true
|
||||
@@ -12,6 +12,7 @@ GET /packages/<name>/~repo?q=<query> # search within a package's files
|
||||
GET /packages/<name>/~artifact/ # page listing repo artifacts by date
|
||||
GET /packages/<name>/~artifact/<id> # page for a specific artifact and status/logs
|
||||
|
||||
## API
|
||||
POST /api/pkg # create repo
|
||||
GET /api/pkg/<name> # repo status/metadata
|
||||
POST /api/pkg/<name>/push # upload source tarball
|
||||
@@ -0,0 +1,82 @@
|
||||
use common::build::BuildError;
|
||||
use rocket::response::Responder;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Responder)]
|
||||
pub enum ApiError {
|
||||
#[response(status = 404)]
|
||||
NotFound(String),
|
||||
|
||||
#[response(status = 500)]
|
||||
InternalServerError(()),
|
||||
|
||||
#[response(status = 500)]
|
||||
ServerError(String),
|
||||
|
||||
#[response(status = 401)]
|
||||
Unauthorized(String),
|
||||
#[response(status = 403)]
|
||||
Forbidden(String),
|
||||
|
||||
#[response(status = 400)]
|
||||
BadRequest(String),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ApiError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
ApiError::InternalServerError(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BuildError> for ApiError {
|
||||
fn from(err: BuildError) -> Self {
|
||||
match err {
|
||||
BuildError::IoError(err) => ApiError::ServerError(err.to_string()),
|
||||
BuildError::Generic(err) => ApiError::ServerError(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DsxError> for ApiError {
|
||||
fn from(err: DsxError) -> Self {
|
||||
ApiError::ServerError(format!("{:?}: {}", err.r#type, err.message))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DsxError {
|
||||
pub message: String,
|
||||
pub r#type: ErrorType,
|
||||
}
|
||||
|
||||
impl DsxError {
|
||||
pub fn new(message: impl AsRef<str>) -> Self {
|
||||
DsxError {
|
||||
message: message.as_ref().to_string(),
|
||||
r#type: ErrorType::Generic,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_context(message: impl AsRef<str>, r#type: ErrorType) -> Self {
|
||||
DsxError {
|
||||
message: message.as_ref().to_string(),
|
||||
r#type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for DsxError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Self::with_context(err.to_string(), ErrorType::IoError)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub enum ErrorType {
|
||||
BuildFailed,
|
||||
IoError,
|
||||
|
||||
#[default]
|
||||
Generic,
|
||||
TarError,
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use rocket::Data;
|
||||
use rocket::data::ToByteUnit;
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::{fs::FileServer, serde::Deserialize};
|
||||
|
||||
use rocket_dyn_templates::tera::{Function, Value};
|
||||
use rocket_dyn_templates::{Template, context, tera};
|
||||
|
||||
use dotenv::dotenv;
|
||||
use serde::Serialize;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::error::ApiError;
|
||||
use crate::model::{Package, PackageMeta};
|
||||
use dsx_common::config::DsxConfig;
|
||||
|
||||
mod error;
|
||||
mod model;
|
||||
|
||||
mod routes;
|
||||
|
||||
fn language_colour() -> impl Function {
|
||||
Box::new(
|
||||
move |args: &std::collections::HashMap<String, Value>| -> tera::Result<Value> {
|
||||
match args.get("lang") {
|
||||
Some(Value::String(lang)) => match lang.as_str() {
|
||||
// language syntax colour
|
||||
"dsc" => Ok(Value::String("#000000".to_string())),
|
||||
"dsa" => Ok(Value::String("#3776AB".to_string())),
|
||||
_ => Ok(Value::String("#FFFFFF".to_string())),
|
||||
},
|
||||
Some(_) => Err(tera::Error::msg("Invalid argument type")),
|
||||
None => Ok(Value::String("#FFFFFF".to_string())),
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
if dotenv().is_err() {
|
||||
eprintln!("Failed to load .env file");
|
||||
}
|
||||
|
||||
use routes::api;
|
||||
use routes::pages;
|
||||
|
||||
rocket::build()
|
||||
.mount(
|
||||
"/packages",
|
||||
routes![
|
||||
pages::search_packages,
|
||||
pages::package_main,
|
||||
pages::repo_file,
|
||||
pages::search_repo_files,
|
||||
pages::list_artifacts,
|
||||
pages::artifact_detail,
|
||||
],
|
||||
)
|
||||
.mount(
|
||||
"/api",
|
||||
routes![
|
||||
api::create_repo,
|
||||
api::get_pkg,
|
||||
api::push_tarball,
|
||||
api::pull_tarball,
|
||||
api::download_artifact
|
||||
],
|
||||
)
|
||||
.attach(Template::custom(|tera| {
|
||||
tera.tera
|
||||
.register_function("language_colour", language_colour());
|
||||
}))
|
||||
.mount("/static", FileServer::from("./static"))
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use chrono::Utc;
|
||||
use dsx_common::builder::{self, BuildContext};
|
||||
use dsx_common::config::DsxConfig;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
pub static DATA_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
PathBuf::from(std::env::var("DATA_DIR").unwrap_or("./data".to_string()))
|
||||
});
|
||||
|
||||
use crate::error::{ApiError, DsxError, ErrorType};
|
||||
|
||||
// stored as a Package.toml above the repository root.
|
||||
// not directly modifiable by the user/maintainer.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PackageMeta {
|
||||
pub id: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub latest_build_date: Option<String>,
|
||||
#[serde(default)]
|
||||
pub latest_build_status: Option<String>,
|
||||
#[serde(default)]
|
||||
pub latest_build_id: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub detected_languages: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Package {
|
||||
pub config: DsxConfig,
|
||||
pub language: String,
|
||||
pub meta: PackageMeta,
|
||||
pub files: Vec<FileObj>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileObj {
|
||||
pub name: String,
|
||||
pub path: String,
|
||||
pub is_dir: bool,
|
||||
pub size: u64,
|
||||
pub extension: String,
|
||||
}
|
||||
|
||||
enum BuildStatus {
|
||||
// if we don't know the build state, or no build has run before.
|
||||
None,
|
||||
|
||||
// waiting for build
|
||||
Pending,
|
||||
|
||||
// build
|
||||
Building,
|
||||
|
||||
// build result
|
||||
Success,
|
||||
Failure,
|
||||
Error,
|
||||
}
|
||||
|
||||
pub struct PackageHandle {
|
||||
// id of the package
|
||||
pub package_id: String,
|
||||
|
||||
config: Option<DsxConfig>,
|
||||
meta: Option<PackageMeta>,
|
||||
|
||||
// build info
|
||||
build_state: BuildStatus,
|
||||
}
|
||||
|
||||
impl PackageHandle {
|
||||
const META_PATH: &'static str = "Package.toml";
|
||||
const CONFIG_PATH: &'static str = "repo/Dsx.toml";
|
||||
const ARTIFACTS_DIR: &'static str = "artifacts";
|
||||
|
||||
pub fn new(package_id: impl Into<String>) -> Self {
|
||||
PackageHandle {
|
||||
package_id: package_id.into(),
|
||||
config: None,
|
||||
meta: None,
|
||||
build_state: BuildStatus::None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(&mut self) -> Result<&mut Self, DsxError> {
|
||||
self.get_config()?;
|
||||
self.get_meta()?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn tarball(&self) -> Result<Vec<u8>, DsxError> {
|
||||
let src_dir = self.path().join("repo");
|
||||
pack_tarball(&src_dir)
|
||||
}
|
||||
|
||||
pub fn unpack(&mut self, archive: &Path) -> Result<&mut Self, DsxError> {
|
||||
let dest = DATA_DIR.join("repos").join(&self.package_id).join("repo");
|
||||
unpack_tarball(archive, &dest)?;
|
||||
self.load()
|
||||
}
|
||||
|
||||
pub fn path(&self) -> PathBuf {
|
||||
DATA_DIR.join("repos").join(&self.package_id)
|
||||
}
|
||||
|
||||
pub fn build(&mut self) -> Result<&mut Self, DsxError> {
|
||||
let ctx = BuildContext {
|
||||
project_dir: self.path().join("repo"),
|
||||
build_dir: self.path().join("build"),
|
||||
artifact_dir: self.path().join("artifacts"),
|
||||
};
|
||||
|
||||
self.build_state = BuildStatus::Building;
|
||||
|
||||
let id = self.package_id.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let id = id;
|
||||
|
||||
let res = match builder::build_project(ctx) {
|
||||
Ok(_) => "success",
|
||||
Err(_) => "failure",
|
||||
};
|
||||
|
||||
let mut handle = PackageHandle::new(&id);
|
||||
let mut meta = handle.get_meta().unwrap();
|
||||
meta.latest_build_date =
|
||||
Some(Utc::now().format("%Y-%m-%d %H:%M:%S").to_string());
|
||||
meta.latest_build_status = Some(res.to_string());
|
||||
meta.latest_build_id = Some(id);
|
||||
handle.set_meta(meta)
|
||||
});
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn get_config(&mut self) -> Result<DsxConfig, DsxError> {
|
||||
let config_path = self.path().join(Self::CONFIG_PATH);
|
||||
|
||||
let config = fs::read_to_string(&config_path).map_err(|e| {
|
||||
warn!("unable to read Dsx.toml, {e}");
|
||||
DsxError::with_context("Unable to read Dsx.toml", ErrorType::IoError)
|
||||
})?;
|
||||
|
||||
let config: DsxConfig = toml::from_str(&config).map_err(|e| {
|
||||
warn!("unable to parse Dsx.toml: {e}");
|
||||
DsxError::new("Unable to parse Dsx.toml")
|
||||
})?;
|
||||
|
||||
self.config = Some(config.clone());
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub fn get_meta(&mut self) -> Result<PackageMeta, DsxError> {
|
||||
let meta_path = self.path().join(Self::META_PATH);
|
||||
|
||||
let meta = fs::read_to_string(&meta_path).map_err(|e| {
|
||||
warn!("unable to read Package.toml, {e}");
|
||||
DsxError::with_context("Unable to read Package.toml", ErrorType::IoError)
|
||||
})?;
|
||||
|
||||
let meta: PackageMeta = toml::from_str(&meta).map_err(|e| {
|
||||
warn!("unable to parse Package.toml: {e}");
|
||||
DsxError::new("Unable to parse Package.toml")
|
||||
})?;
|
||||
|
||||
self.meta = Some(meta.clone());
|
||||
Ok(meta)
|
||||
}
|
||||
|
||||
pub fn set_meta(&mut self, meta: PackageMeta) -> Result<(), DsxError> {
|
||||
self.meta = Some(meta);
|
||||
self.save_meta()
|
||||
}
|
||||
|
||||
fn save_meta(&self) -> Result<(), DsxError> {
|
||||
let meta_path = self.path().join(Self::META_PATH);
|
||||
let str = toml::to_string(&self.meta.as_ref().unwrap()).unwrap();
|
||||
|
||||
fs::write(&meta_path, str).map_err(|e| {
|
||||
warn!("unable to write Package.toml, {e}");
|
||||
DsxError::with_context("Unable to write Package.toml", ErrorType::IoError)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_languages(&mut self) -> Result<Vec<String>, DsxError> {
|
||||
self.get_meta()?;
|
||||
|
||||
if let Some(languages) = &self.meta.as_ref().unwrap().detected_languages {
|
||||
return Ok(languages.clone());
|
||||
}
|
||||
|
||||
let mut langs = Vec::new();
|
||||
|
||||
for entry in WalkDir::new(self.path().join("repo/src"))
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
if entry.file_type().is_file() {
|
||||
let path = entry.path();
|
||||
let extension = path.extension().and_then(|ext| ext.to_str());
|
||||
if let Some(ext) = extension {
|
||||
match ext {
|
||||
"dsa" | "dsc" => langs.push(ext.to_string()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.meta.as_mut().unwrap().detected_languages = Some(langs.clone());
|
||||
|
||||
Ok(langs)
|
||||
}
|
||||
|
||||
pub fn get_artifact(&self) -> Result<Vec<u8>, DsxError> {
|
||||
println!(
|
||||
"{}",
|
||||
self.path()
|
||||
.join(Self::ARTIFACTS_DIR)
|
||||
.join("out.dsb")
|
||||
.display()
|
||||
);
|
||||
|
||||
let artifact_path = self.path().join(Self::ARTIFACTS_DIR).join("out.dsb");
|
||||
fs::read(artifact_path).map_err(|e| {
|
||||
warn!("unable to read artifact for repo: {e}");
|
||||
DsxError::new("Unable to read artifact")
|
||||
})
|
||||
}
|
||||
|
||||
pub fn file_tree(&self, subpath: impl AsRef<Path>) -> Result<Vec<FileObj>, DsxError> {
|
||||
let repo = self.path().join("repo").join(subpath);
|
||||
|
||||
let dir = fs::read_dir(repo).map_err(|e| {
|
||||
warn!("unable to read files for repo, {e}");
|
||||
DsxError::new("Unable to read files in repository")
|
||||
})?;
|
||||
|
||||
let mut files = Vec::new();
|
||||
|
||||
// for entry in WalkDir::new(repo_path.join("repo")).max_depth(1) {
|
||||
for entry in dir {
|
||||
let entry = entry.map_err(|e| {
|
||||
warn!("unable to read file entry for repo, {e}");
|
||||
DsxError::new("Internal error")
|
||||
})?;
|
||||
|
||||
// remove root of DATA_DIR/repos/<name>/repo
|
||||
let path = entry.path();
|
||||
|
||||
let metadata = fs::metadata(&path).map_err(|e| {
|
||||
warn!("unable to read file metadata for repo, {e}");
|
||||
DsxError::new("Internal error")
|
||||
})?;
|
||||
let is_dir = metadata.is_dir();
|
||||
let size = metadata.len();
|
||||
let extension = path
|
||||
.extension()
|
||||
.map_or(String::new(), |ext| ext.to_string_lossy().to_string());
|
||||
files.push(FileObj {
|
||||
path: path
|
||||
.strip_prefix(self.path().join("repo"))
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string(),
|
||||
name: path.file_name().unwrap().to_string_lossy().to_string(),
|
||||
is_dir,
|
||||
size,
|
||||
extension,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(files)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tar helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
use flate2::read::GzDecoder;
|
||||
use flate2::{Compression, write::GzEncoder};
|
||||
use std::fs::File;
|
||||
use tar::Builder;
|
||||
|
||||
fn unpack_tarball(
|
||||
archive: &std::path::Path,
|
||||
dest: &std::path::Path,
|
||||
) -> Result<(), DsxError> {
|
||||
let file = File::open(archive)
|
||||
.map_err(|e| DsxError::with_context(e.to_string(), ErrorType::IoError))?;
|
||||
|
||||
fs::create_dir_all(dest)?;
|
||||
|
||||
let gz = GzDecoder::new(file);
|
||||
let mut tar = tar::Archive::new(gz);
|
||||
tar.unpack(dest)
|
||||
.map_err(|e| DsxError::with_context(e.to_string(), ErrorType::TarError))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_tarball(src_dir: &std::path::Path) -> Result<Vec<u8>, DsxError> {
|
||||
let buf = Vec::new();
|
||||
let gz = GzEncoder::new(buf, Compression::default());
|
||||
let mut tar = Builder::new(gz);
|
||||
tar.append_dir_all(".", src_dir)?;
|
||||
let gz = tar.into_inner()?;
|
||||
Ok(gz.finish()?)
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
use std::fs;
|
||||
|
||||
use dsx_common::config::DsxConfig;
|
||||
use rocket::{Data, data::ToByteUnit, serde::json::Json};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
model::{DATA_DIR, PackageHandle, PackageMeta},
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct NewRepo<'r> {
|
||||
name: &'r str,
|
||||
}
|
||||
|
||||
// Create repo
|
||||
#[post("/pkg", data = "<repo>")]
|
||||
pub fn create_repo(repo: Json<NewRepo<'_>>) -> Result<(), &'static str> {
|
||||
let path = DATA_DIR.join("repos").join(repo.name);
|
||||
|
||||
if repo.name.is_empty() {
|
||||
return Err("Repository name cannot be empty!");
|
||||
}
|
||||
|
||||
if path.exists() {
|
||||
tracing::info!(
|
||||
"Attempt to create repository '{}' which already exists.",
|
||||
repo.name
|
||||
);
|
||||
return Err("This repository already exists!");
|
||||
}
|
||||
|
||||
if let Err(e) = fs::create_dir_all(path) {
|
||||
tracing::error!(
|
||||
"Attempted to create package with name {} - Error: {e},",
|
||||
repo.name
|
||||
);
|
||||
return Err("Internal server error");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Repo status/metadata
|
||||
#[get("/pkg/<id>")]
|
||||
pub fn get_pkg(id: &str) -> Result<Json<(PackageMeta, DsxConfig)>, ApiError> {
|
||||
let mut handle = PackageHandle::new(id);
|
||||
let meta = handle.get_meta()?;
|
||||
let config = handle.get_config()?;
|
||||
Ok(Json((meta, config)))
|
||||
}
|
||||
|
||||
// Upload source tarball
|
||||
#[post("/pkg/<id>/push", data = "<data>")]
|
||||
pub async fn push_tarball(id: &str, data: Data<'_>) -> Result<(), ApiError> {
|
||||
let mut handle = PackageHandle::new(id);
|
||||
let repo = handle.path();
|
||||
|
||||
let tmp_path = repo.join("upload.tar.gz");
|
||||
let stream = data
|
||||
.open(256.mebibytes())
|
||||
.into_file(&tmp_path)
|
||||
.await
|
||||
.map_err(|e| ApiError::ServerError(e.to_string()))?;
|
||||
|
||||
if !stream.is_complete() {
|
||||
return Err(ApiError::BadRequest("Incomplete upload".to_string()));
|
||||
}
|
||||
|
||||
// Unpack over the existing repo dir.
|
||||
// if the repo is already deleted that's fine so ignore err
|
||||
if handle.path().exists() {
|
||||
let _ = fs::remove_dir_all(repo.join("repo"));
|
||||
}
|
||||
|
||||
handle.unpack(&tmp_path)?;
|
||||
fs::remove_file(&tmp_path).ok();
|
||||
|
||||
// we don't care if there's an error in the build.
|
||||
let _ = handle.build();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Download source tarball
|
||||
#[get("/pkg/<id>/pull")]
|
||||
pub fn pull_tarball(
|
||||
id: &str,
|
||||
) -> Result<(rocket::http::ContentType, Vec<u8>), Json<ApiError>> {
|
||||
if let Ok(tarball) = PackageHandle::new(id).tarball() {
|
||||
Ok((
|
||||
rocket::http::ContentType::new("application", "octet-stream"),
|
||||
tarball,
|
||||
))
|
||||
} else {
|
||||
Err(Json(ApiError::NotFound(format!(
|
||||
"repo with id {id} does not exist"
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
// Download compiled binary
|
||||
#[get("/pkg/<id>/artifact")]
|
||||
pub fn download_artifact(
|
||||
id: &str,
|
||||
) -> Result<(rocket::http::ContentType, Vec<u8>), Json<ApiError>> {
|
||||
if let Ok(artifact) = PackageHandle::new(id).get_artifact() {
|
||||
Ok((
|
||||
rocket::http::ContentType::new("application", "octet-stream"),
|
||||
artifact,
|
||||
))
|
||||
} else {
|
||||
Err(Json(ApiError::NotFound(format!(
|
||||
"repo with id {id} does not have a valid artifact"
|
||||
))))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod api;
|
||||
pub mod pages;
|
||||
@@ -0,0 +1,145 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use dsx_common::config::DsxConfig;
|
||||
use rocket_dyn_templates::{Template, context};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{
|
||||
error::ApiError,
|
||||
model::{DATA_DIR, PackageHandle},
|
||||
};
|
||||
|
||||
// Search for a package
|
||||
#[get("/?<q>")]
|
||||
pub fn search_packages(q: Option<String>) -> Result<Template, ApiError> {
|
||||
#[derive(Serialize)]
|
||||
struct Package {
|
||||
name: String,
|
||||
description: String,
|
||||
updated_at: String,
|
||||
}
|
||||
|
||||
let mut packages = Vec::new();
|
||||
|
||||
let dir = match fs::read_dir(DATA_DIR.join("repos")) {
|
||||
Ok(dir) => dir,
|
||||
Err(e) => {
|
||||
warn!("failed to read repos directory: {}", e);
|
||||
return Err(ApiError::InternalServerError(()));
|
||||
}
|
||||
};
|
||||
|
||||
for entry in dir {
|
||||
let entry = entry.map_err(|e| {
|
||||
warn!("failed to read entry: {}", e);
|
||||
ApiError::InternalServerError(())
|
||||
})?;
|
||||
|
||||
let config_path = entry.path().join("repo").join("Dsx.toml");
|
||||
if config_path.exists() {
|
||||
let text = fs::read_to_string(&config_path).map_err(|e| {
|
||||
warn!("failed to read config file: {}", e);
|
||||
ApiError::InternalServerError(())
|
||||
})?;
|
||||
let config: DsxConfig = toml::from_str(&text).map_err(|e| {
|
||||
warn!("failed to parse config file: {}", e);
|
||||
ApiError::InternalServerError(())
|
||||
})?;
|
||||
|
||||
error!("{}", config.description.clone().unwrap_or_default());
|
||||
|
||||
// skip repo if it doesnt match query params
|
||||
if let Some(query) = &q
|
||||
&& !(config.name.contains(query)
|
||||
|| config
|
||||
.description
|
||||
.clone()
|
||||
.unwrap_or_default()
|
||||
.contains(query))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
packages.push(Package {
|
||||
name: config.name,
|
||||
description: config.description.unwrap_or_default(),
|
||||
updated_at: String::from("0:00"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Template::render(
|
||||
"packages",
|
||||
context! {
|
||||
packages,
|
||||
query: q.clone().unwrap_or_default()
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
// Main page for a repository, shows status, files, name etc.
|
||||
#[get("/<id>")]
|
||||
pub fn package_main(id: &str) -> Result<Template, ApiError> {
|
||||
// get package info
|
||||
|
||||
let mut handle = PackageHandle::new(id);
|
||||
let meta = handle.get_meta()?;
|
||||
let config = handle.get_config()?;
|
||||
let files = handle.file_tree("")?;
|
||||
|
||||
let parent_path: Option<String> = None;
|
||||
let current_path: Option<String> = None;
|
||||
|
||||
Ok(Template::render(
|
||||
"package_home",
|
||||
context! {
|
||||
parent_path,
|
||||
current_path,
|
||||
meta: meta,
|
||||
config: config,
|
||||
files: files,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
// Path for a file within a repo
|
||||
#[get("/<id>/~repo/<path..>", rank = 1)]
|
||||
pub fn repo_file(id: &str, path: std::path::PathBuf) -> Result<Template, ApiError> {
|
||||
// get package info
|
||||
let mut handle = PackageHandle::new(id);
|
||||
let meta = handle.get_meta()?;
|
||||
let config = handle.get_config()?;
|
||||
let files = handle.file_tree(&path)?;
|
||||
|
||||
let parent_path = path.parent().unwrap_or(Path::new(""));
|
||||
let current_path = &path;
|
||||
|
||||
Ok(Template::render(
|
||||
"package_home",
|
||||
context! {
|
||||
parent_path,
|
||||
current_path,
|
||||
meta: meta,
|
||||
config: config,
|
||||
files: files,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
// Search within a package's files
|
||||
#[get("/<name>/~repo?<q>", rank = 2)]
|
||||
pub fn search_repo_files(name: &str, q: Option<String>) -> String {
|
||||
format!("Search within {} for {:?}", name, q)
|
||||
}
|
||||
|
||||
// Page listing repo artifacts by date
|
||||
#[get("/<name>/artifacts")]
|
||||
pub fn list_artifacts(name: &str) -> String {
|
||||
format!("Artifacts for package {}", name)
|
||||
}
|
||||
|
||||
// Page for a specific artifact and status/logs
|
||||
#[get("/<name>/artifacts/<id>")]
|
||||
pub fn artifact_detail(name: &str, id: u64) -> String {
|
||||
format!("Artifact {} details for package {}", id, name)
|
||||
}
|
||||
|
Before Width: | Height: | Size: 201 KiB After Width: | Height: | Size: 201 KiB |
+5
-6
@@ -4,17 +4,17 @@
|
||||
<div class="file-toolbar">
|
||||
<div class="file-toolbar-search">
|
||||
<svg class="file-toolbar-search-icon" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
|
||||
<form method="get" action="/packages/{{ package.config.name }}/~repo">
|
||||
<form method="get" action="/packages/{{ meta.id }}/~repo">
|
||||
<input type="text" name="q" placeholder="Filter files…" value="{{ query | default(value='') }}">
|
||||
</form>
|
||||
</div>
|
||||
<span class="dim small mono">{{ current_path | default(value="/") }}</span>
|
||||
</div>
|
||||
|
||||
{% if package.files and package.files | length > 0 %}
|
||||
{% if files and files | length > 0 %}
|
||||
<div class="file-tree-body">
|
||||
{% if current_path %}
|
||||
<a class="file-row" href="/packages/{{ package.config.name }}/~repo/{{ parent_path | default(value='') }}">
|
||||
<a class="file-row" href="/packages/{{ meta.id }}/~repo/{{ parent_path | default(value='') }}">
|
||||
<span class="file-icon">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z"/></svg>
|
||||
</span>
|
||||
@@ -23,9 +23,8 @@
|
||||
{% endif %}
|
||||
|
||||
|
||||
|
||||
{% for file in package.files %}
|
||||
{{ files::file_view(package = package.config.name, file = file) }}
|
||||
{% for file in files %}
|
||||
{{ files::file_view(package = meta.id, file = file) }}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% else %}
|
||||
+14
-11
@@ -6,25 +6,28 @@
|
||||
<div class="sidebar-card-title">Latest Build</div>
|
||||
<div class="sidebar-card-body">
|
||||
<div class="build-status-row">
|
||||
{% if package.meta.latest_build_status == "success" %}
|
||||
{% if meta.latest_build_status == "success" %}
|
||||
<span class="badge badge-success"><span class="dot"></span>passing</span>
|
||||
{% elif package.meta.latest_build_status == "failure" %}
|
||||
{% elif meta.latest_build_status == "failure" %}
|
||||
<span class="badge badge-failure"><span class="dot"></span>failing</span>
|
||||
{% elif package.meta.latest_build_status == "running" %}
|
||||
{% elif meta.latest_build_status == "running" %}
|
||||
<span class="badge badge-running"><span class="dot dot-pulse"></span>running</span>
|
||||
{% elif package.meta.latest_build_status == "pending" %}
|
||||
{% elif meta.latest_build_status == "pending" %}
|
||||
<span class="badge badge-pending"><span class="dot"></span>pending</span>
|
||||
{% else %}
|
||||
<span class="dim small">No builds yet</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if package.meta.latest_build_id %}
|
||||
<a class="build-link" href="/packages/{{ package.config.name }}/~artifact/{{ package.meta.latest_build_id }}">
|
||||
#{{ package.meta.latest_build_id }} — view logs →
|
||||
{% if meta.latest_build_id %}
|
||||
<a class="build-link"
|
||||
href="/api/pkg/{{ config.name }}/artifact"
|
||||
download="{{ config.name }}-{{ meta.latest_build_date }}.dsb"
|
||||
style="font-size:13px">
|
||||
"{{ config.name }}-{{ meta.latest_build_date }}.dsb"
|
||||
</a>
|
||||
{% endif %}
|
||||
{% if package.meta.latest_build_date %}
|
||||
<div class="dim small mono" style="margin-top:6px">{{ package.meta.latest_build_date }}</div>
|
||||
{% if meta.latest_build_date %}
|
||||
<div class="dim small mono" style="margin-top:6px">{{ meta.latest_build_date }}</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
@@ -67,11 +70,11 @@
|
||||
<div class="card sidebar-card">
|
||||
<div class="sidebar-card-title">Links</div>
|
||||
<div class="sidebar-card-body" style="display:flex;flex-direction:column;gap:6px">
|
||||
<a href="/packages/{{ package.config.name }}/~repo" style="font-size:13px">
|
||||
<a href="/packages/{{ config.name }}/~repo" style="font-size:13px">
|
||||
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="vertical-align:middle;margin-right:4px"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
|
||||
Browse files
|
||||
</a>
|
||||
<a href="/packages/{{ package.config.name }}/~artifact/" style="font-size:13px">
|
||||
<a href="/packages/{{ config.name }}/~artifact/" style="font-size:13px">
|
||||
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="vertical-align:middle;margin-right:4px"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
|
||||
Build history
|
||||
</a>
|
||||
+9
-9
@@ -1,7 +1,7 @@
|
||||
{% extends "base" %}
|
||||
{% import 'components/file' as files %}
|
||||
|
||||
{% block title %}{{ package.config.name }}{% endblock %}
|
||||
{% block title %}{{ config.name }}{% endblock %}
|
||||
|
||||
{% block extra_head %}
|
||||
<style>
|
||||
@@ -171,24 +171,24 @@
|
||||
<div class="breadcrumb">
|
||||
<a href="/packages/">packages</a>
|
||||
<span class="breadcrumb-sep">/</span>
|
||||
<span>{{ package.config.name }}</span>
|
||||
<span>{{ config.name }}</span>
|
||||
</div>
|
||||
|
||||
<div class="pkg-header">
|
||||
<h1>
|
||||
{{ package.config.name }}
|
||||
{% if package.meta.latest_build_status == "success" %}
|
||||
{{ config.name }}
|
||||
{% if meta.latest_build_status == "success" %}
|
||||
<span class="badge badge-success"><span class="dot"></span>passing</span>
|
||||
{% elif package.meta.latest_build_status == "failure" %}
|
||||
{% elif meta.latest_build_status == "failure" %}
|
||||
<span class="badge badge-failure"><span class="dot"></span>failing</span>
|
||||
{% elif package.meta.latest_build_status == "running" %}
|
||||
{% elif meta.latest_build_status == "running" %}
|
||||
<span class="badge badge-running"><span class="dot dot-pulse"></span>running</span>
|
||||
{% elif package.meta.latest_build_status == "pending" %}
|
||||
{% elif meta.latest_build_status == "pending" %}
|
||||
<span class="badge badge-pending"><span class="dot"></span>pending</span>
|
||||
{% endif %}
|
||||
</h1>
|
||||
{% if package.config.description %}
|
||||
<p class="pkg-desc">{{ package.config.description }}</p>
|
||||
{% if config.description %}
|
||||
<p class="pkg-desc">{{ config.description }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
id="example"
|
||||
@@ -1 +0,0 @@
|
||||
name = "example"
|
||||
@@ -1,39 +0,0 @@
|
||||
|
||||
// GENERATED BY DSX-BUILD
|
||||
// Generated at: 2026-02-21 02:50:14
|
||||
// Project name: example
|
||||
|
||||
// Imports
|
||||
include print: "./lib/print.dsa"
|
||||
|
||||
// Globals & Reserved Memory
|
||||
dw stack: 0x10000
|
||||
db message: "Process Exited with code:"
|
||||
|
||||
// Entry Point
|
||||
_init:
|
||||
ldw stack, bpr
|
||||
mov bpr, spr
|
||||
push zero
|
||||
call main
|
||||
call print::print_newline
|
||||
lwi message, rg0
|
||||
push rg0
|
||||
call print::print
|
||||
pop zero
|
||||
call print::print_hex_word
|
||||
pop zero
|
||||
hlt
|
||||
|
||||
main:
|
||||
push bpr
|
||||
mov spr, bpr
|
||||
|
||||
// Your code goes here
|
||||
|
||||
// Return zero
|
||||
stw zero, bpr, 8
|
||||
|
||||
mov bpr, spr
|
||||
pop bpr
|
||||
return
|
||||
@@ -1,53 +0,0 @@
|
||||
use std::process::{Command, Stdio};
|
||||
use std::{
|
||||
env, fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use dsx::common::builder;
|
||||
|
||||
pub mod new;
|
||||
|
||||
fn main() {
|
||||
// Very small CLI – only three sub‑commands.
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: dsx-build <new|build|package> [options]");
|
||||
std::process::exit(1);
|
||||
}
|
||||
match args[1].as_str() {
|
||||
"new" => new::new_project(&args[2..]),
|
||||
"build" => {
|
||||
if let Some(dir) = find_project_root() {
|
||||
builder::build_project(&dir).expect("Build failed!");
|
||||
} else {
|
||||
eprintln!("No Dsx.toml found");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
"package" => todo!("Package manager stub – not implemented yet."),
|
||||
_ => {
|
||||
eprintln!("Unknown command: {}", args[1]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_project_root() -> Option<PathBuf> {
|
||||
// check if current dir has Dsx.toml otherwise check parent dir recursively
|
||||
let mut cwd = env::current_dir().unwrap();
|
||||
loop {
|
||||
let dsx_toml = cwd.join("Dsx.toml");
|
||||
if dsx_toml.exists() {
|
||||
return Some(cwd);
|
||||
}
|
||||
|
||||
if let Some(parent) = cwd.parent() {
|
||||
cwd = parent.to_path_buf();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user