Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from ArnabRollin/bytecode
Browse files Browse the repository at this point in the history
Added bytecode compiler + runner
  • Loading branch information
ArnabRollin authored Oct 26, 2023
2 parents 5630c50 + 4fa5724 commit 6484f67
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 54 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.10.0] - 2023-10-26

### Added

- Variable-options in CLI
- Bytecode compiler and runner (level 1)
- Better documentation in CLI.

### Changed

- Now there are two runner functions: `run_tokens` and `run` (which tokenizes the line and calls `run_tokens` with the tokens)
- Integers are now `i64` and floats are `f64`

## [0.9.0] - 2023-10-24

### Added
Expand Down Expand Up @@ -111,7 +124,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Simple function and string parsing

[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.9.0...HEAD
[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.10.0...HEAD

[0.10.0]: https://github.com/ArnabRollin/dwn/compare/v0.9.0...v0.10.0

[0.9.0]: https://github.com/ArnabRollin/dwn/compare/v0.8.0...v0.9.0

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dwn"
version = "0.9.0"
version = "0.10.0"
edition = "2021"

[dependencies]
Expand Down
69 changes: 65 additions & 4 deletions src/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@
/// ```
pub fn about() {
let about = r#"
Usage: dwn [options...] [command] [args...]
Dawn (dwn) is the interpreter and bytecode compiler for the Dawn Programming Language.
Usage:
dwn -h | --help
dwn -v | --version
dwn help (<command>)
dwn run (<path_to_file>)
dwn bytec | bytecodec | bytc (<path_to_file>) [--level=<lvl>]
dwn byterun | bytecoderun | bytrun (<path_to_bytecode>)
dwn idle
dwn framework | fw
Options:
-h --help Show this message and exit.
-v --version Show the version and exit.
--level=<lvl> Set the level of the engine which bytecode compiles the file.
Use `latest` (case insensitive) to set to the latest engine. [default: LATEST]
"#
.trim();

Expand All @@ -29,13 +43,60 @@ pub fn about() {
/// ```
pub fn help(command: Option<&String>) {
let info_help: &str = r#"
Usage: dwn help [command]
Shows information about a command and how to use it.
"#
Usage: dwn help (<command>)
"#
.trim();
let info_run: &str = r#"
Runs a Dawn project file.
Usage: dwn run (<path_to_file>)
"#
.trim();
let info_bytec: &str = r#"
Bytecode compiles a Dawn project file.
Usage:
dwn bytec (<path_to_file>)
dwn bytecodec (<path_to_file>)
dwn bytc (<path_to_file>)
dwn bytec (<path_to_file>) [--level=<lvl>]
dwn bytecodec (<path_to_file>) [--level=<lvl>]
dwn bytc (<path_to_file>) [--level=<lvl>]
"#
.trim();
let info_byterun: &str = r#"
Runs a Dawn bytecode file.
Usage:
dwn byterun (<path_to_file>)
dwn bytecoderun (<path_to_file>)
dwn bytrun (<path_to_file>)
"#
.trim();
let info_idle: &str = r#"
Starts the Integrated Development and Learning Environment (IDLE)
Usage: dwn idle
"#
.trim();
let info_framework: &str = r#"
Creates a framework for Dawn Programming Language extensions.
Usage:
dwn framework
dwn fw
"#
.trim();

match command.unwrap_or(&String::new()).as_str() {
"help" => println!("{}", info_help),
"run" => println!("{}", info_run),
"bytec" | "bytecodec" | "bytc" => println!("{}", info_bytec),
"byterun" | "bytecoderun" | "bytrun" => println!("{}", info_byterun),
"idle" => println!("{}", info_idle),
"framework" | "fw" => println!("{}", info_framework),
command => println!("Unknown command: {}", command),
}
}
51 changes: 40 additions & 11 deletions src/argparser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Argument parser for Dawn (dwn).
use std::env::Args;
use std::{collections::HashMap, env::Args};

/// Parses the arguments.
///
Expand All @@ -24,14 +24,24 @@ pub fn argparse(mut args: Args) -> Arguments {
let mut options: Vec<String> = vec![];
let mut flags: Vec<String> = vec![];
let mut arguments: Vec<String> = vec![];
let mut variables: HashMap<String, String> = HashMap::new();

while let Some(arg) = args.next() {
if arg.starts_with("--") {
options.extend(
arg.trim_start_matches("--")
.split('-')
.map(|s| s.to_string()),
);
let varsplit: Vec<&str> = arg.split('=').collect();

if varsplit[..varsplit.len() - 1].is_empty() {
options.extend(
arg.trim_start_matches("--")
.split('-')
.map(|s| s.to_string()),
);
} else {
variables.insert(
varsplit[0].trim_start_matches("--").to_string(),
varsplit[1].to_string(),
);
}
} else if arg.starts_with("-") {
flags.extend(arg.chars().skip(1).map(|c| c.to_string()));
} else {
Expand All @@ -50,6 +60,7 @@ pub fn argparse(mut args: Args) -> Arguments {
flags,
command,
arguments,
variables,
}
}

Expand All @@ -62,13 +73,15 @@ pub fn argparse(mut args: Args) -> Arguments {
/// flags: vec![String::from("f")],
/// command: String::from("lawn"),
/// arguments: vec![String::from("dig")],
/// variables: vec![],
/// }
/// ```
pub struct Arguments {
pub options: Vec<String>,
pub flags: Vec<String>,
pub command: String,
pub arguments: Vec<String>,
pub variables: HashMap<String, String>,
}

#[test]
Expand All @@ -77,16 +90,26 @@ fn argument_parser() {
let mut options: Vec<String> = vec![];
let mut flags: Vec<String> = vec![];
let mut arguments: Vec<String> = vec![];
let mut variables: HashMap<String, String> = HashMap::new();

let mut args = args.iter();

while let Some(arg) = args.next() {
if arg.starts_with("--") {
options.extend(
arg.trim_start_matches("--")
.split('-')
.map(|s| s.to_string()),
);
let varsplit: Vec<&str> = arg.split('=').collect();

if varsplit.is_empty() {
options.extend(
arg.trim_start_matches("--")
.split('-')
.map(|s| s.to_string()),
);
} else {
variables.insert(
varsplit[0].trim_start_matches("--").to_string(),
varsplit[1].to_string(),
);
}
} else if arg.starts_with("-") {
flags.extend(arg.chars().skip(1).map(|c| c.to_string()));
} else {
Expand All @@ -105,20 +128,26 @@ fn argument_parser() {
flags,
command,
arguments,
variables,
}
}
let args = argparse(vec![
"klein".to_string(),
"--c-c-c".to_string(),
"-dd".to_string(),
"cey".to_string(),
"--level=1".to_string(),
]);

let mut variables_assert = HashMap::new();
variables_assert.insert("level".to_string(), "1".to_string());

assert_eq!(args.command, "klein".to_string());
assert_eq!(args.arguments, vec!["cey".to_string()]);
assert_eq!(
args.options,
vec!["c".to_string(), "c".to_string(), "c".to_string()]
);
assert_eq!(args.flags, vec!["d".to_string(), "d".to_string()]);
assert_eq!(args.variables, variables_assert);
}
Loading

0 comments on commit 6484f67

Please sign in to comment.