Skip to content

Commit

Permalink
Added <parser> module.
Browse files Browse the repository at this point in the history
  • Loading branch information
irfanghat committed Jul 11, 2024
1 parent 5404389 commit 21ff996
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
66 changes: 66 additions & 0 deletions rust/src/iaas/interpreter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::parser::*;

#[derive(Debug)]
pub struct Interpreter;

impl Interpreter {
pub fn new() -> Self {
Interpreter
}

pub async fn interpret(&self, nodes: Vec<ASTNode>) -> Vec<String> {
let mut results = Vec::new();

for node in nodes {
match node {
ASTNode::ProviderBlock { name, properties } => {
results.push(self.interpret_provider_block(name, properties).await);
}
ASTNode::ResourceBlock {
name,
resource_type,
properties,
} => {
results.push(
self.interpret_resource_block(name, resource_type, properties)
.await,
);
}
}
}
results
}

pub async fn interpret_provider_block(
&self,
name: String,
properties: Vec<(String, ASTValue)>,
) -> String {
let mut result = format!("Provider: {}", name);

for (key, value) in properties {
match value {
ASTValue::StringLiteral(val) => result.push_str(&format!("\n {}: {}", key, val)),
ASTValue::NumberLiteral(val) => result.push_str(&format!("\n {}: {}", key, val)),
}
}
result
}

pub async fn interpret_resource_block(
&self,
name: String,
resource_type: String,
properties: Vec<(String, ASTValue)>,
) -> String {
let mut result = format!("Resource: {} {}", resource_type, name);

for (key, value) in properties {
match value {
ASTValue::StringLiteral(val) => result.push_str(&format!("\n {}: {}", key, val)),
ASTValue::NumberLiteral(val) => result.push_str(&format!("\n {}: {}", key, val)),
}
}
result
}
}
4 changes: 3 additions & 1 deletion rust/src/iaas/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod interpreter;
pub mod lexer;
pub mod parser;
pub mod lexer;
pub mod prelude;
1 change: 1 addition & 0 deletions rust/src/iaas/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use crate::iaas::{interpreter::*, lexer::*, parser::*};
37 changes: 31 additions & 6 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(unused)]

use render_cdk::environment_management::prelude::*;
use render_cdk::iaas::prelude::*;
use render_cdk::resource_management::prelude::*;
use tokio::main;

Expand All @@ -9,19 +10,43 @@ use tokio::main;
async fn main() {
/// Examples
/// 1. Querying for deployed Services.
///
///
/// List all Services.
let services = ServiceManager::list_all_services("20").await;
// let services = ServiceManager::list_all_services("20").await;

/// List all Services by Name and Type.
let services = ServiceManager::find_service_by_name_and_type("whoami", "web_service").await;
// let services = ServiceManager::find_service_by_name_and_type("whoami", "web_service").await;

/// List all Services by Region.
let services = ServiceManager::find_service_by_region("oregon", "10").await;
// let services = ServiceManager::find_service_by_region("oregon", "10").await;

/// List all Services by Environment.
let services = ServiceManager::find_service_by_environment("image", "10").await;
// let services = ServiceManager::find_service_by_environment("image", "10").await;
////////////////////////////////////////////////
///
/// 2. Using Terraform for resource provisioning
let input = r#"
provider "render" {
api_key = "rnd_xxxxXXXXxxxxXXXXxxxXX"
}
resource "render_service" "example" {
name = "example-service"
environment = "production"
replicas = 3
}
"#;

let lexer = Lexer::new(input);
let mut parser = Parser::new(lexer);
let ast = parser.parse();
let interpreter = Interpreter::new();
let results = interpreter.interpret(ast).await;

for result in results {
println!("lol");
println!("{}", result);
}
}

/// Checks for regression of service management functions
Expand Down Expand Up @@ -52,7 +77,7 @@ async fn main() {
/// let services = result.unwrap();
/// assert!(!services.is_empty());
/// }
///
///
/// More tests...
#[cfg(test)]
Expand Down

0 comments on commit 21ff996

Please sign in to comment.