add compile flag

This commit is contained in:
Masato Imai
2022-08-23 11:41:43 +09:00
parent fc7f69d558
commit 0b5caee6f5
4 changed files with 49 additions and 20 deletions

View File

@ -14,4 +14,7 @@ pub struct Args {
#[clap(short, long, takes_value = false)]
pub debug: bool,
#[clap(short, long, takes_value = false)]
pub compile: bool,
}

View File

@ -1,14 +1,16 @@
use std::{
collections::HashMap,
fs,
io::{BufRead, BufReader, Write},
fs::{self, File},
io::{BufRead, BufReader, Read, Write},
net::TcpStream,
sync::{Arc, Mutex},
};
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
use crate::{
args::Args,
config::Config,
config::{Config, ConfigFile},
elliptic_curve::encryption::Encryption,
gpsl::{
self,
@ -21,18 +23,49 @@ use crate::{
};
pub fn start_client(args: Args) {
let mut source =
Source::new(fs::read_to_string(&(args.file.unwrap())).expect("Cannot read file."));
let mut tokenizer = Tokenizer::new();
tokenizer.tokenize(&mut source).unwrap();
let mut parser = gpsl::parser::Parser {
tokenizer,
local_vars: HashMap::new(),
let file = args.file.clone().unwrap();
let file_name = {
let file_name = file.file_name();
file_name.unwrap().to_string_lossy()
};
let functions = parser.functions().unwrap();
let functions = {
if file_name.ends_with(".o") {
let mut file = fs::File::open(args.file.clone().unwrap()).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
let mut d = ZlibDecoder::new(&contents[..]);
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
serde_json::from_str(&s).unwrap()
} else {
let mut source = Source::new(
fs::read_to_string(&(args.file.clone().unwrap())).expect("Cannot read file."),
);
let mut tokenizer = Tokenizer::new();
tokenizer.tokenize(&mut source).unwrap();
let mut parser = gpsl::parser::Parser {
tokenizer,
local_vars: HashMap::new(),
};
parser.functions().unwrap()
}
};
if args.compile {
let functions = serde_json::to_string(&functions).unwrap();
let mut file = File::create(format!("{}.o", file_name)).unwrap();
let mut e = ZlibEncoder::new(Vec::new(), Compression::default());
e.write_all(functions.as_bytes()).unwrap();
file.write_all(&e.finish().unwrap()).unwrap();
return;
}
let mut server_functions: HashMap<String, HashMap<String, Box<Node>>> = HashMap::new();
for function in functions.clone() {
@ -105,7 +138,6 @@ pub fn start_client(args: Args) {
let config = Config::read_or_create();
let mut gpsl = GPSL::new(
source,
Some(functions),
Some(server_functions),
Some(servers),

View File

@ -6,7 +6,6 @@ use crate::gpsl::external_function::{
use crate::gpsl::gpsl_type::GPSLType;
use crate::gpsl::node::*;
use crate::gpsl::permission::Permission;
use crate::gpsl::source::Source;
use crate::gpsl::variable::*;
use log::*;
use primitive_types::U512;
@ -34,7 +33,6 @@ pub struct GPSL {
pub private_key: Option<U512>,
pub public_key: Option<EllipticCurvePoint>,
pub global_variables: Vec<Variable>,
pub source: Source,
pub blocks: VecDeque<Block>,
pub external_func: Vec<
fn(
@ -73,7 +71,6 @@ impl VariableStatus {
impl GPSL {
pub fn new(
source: Source,
functions: Option<HashMap<String, Box<Node>>>,
server_functions: Option<HashMap<String, HashMap<String, Box<Node>>>>,
servers: Option<HashMap<String, Arc<Mutex<TcpStream>>>>,
@ -91,7 +88,6 @@ impl GPSL {
>,
) -> GPSL {
GPSL {
source,
functions,
server_functions,
servers,

View File

@ -8,7 +8,6 @@ use crate::args::Args;
use crate::elliptic_curve::encryption::Encryption;
use crate::gpsl::external_function::{ExternalFuncReturn, ExternalFuncStatus, STD_FUNC};
use crate::gpsl::node::Node;
use crate::gpsl::source::Source;
use crate::gpsl::vm::gpsl::{ServerFunctionCall, GPSL};
fn listen_tcp_server(port: u16) -> TcpStream {
@ -41,7 +40,6 @@ pub fn start_server(args: Args) {
debug!("Received: {:?}", functions);
let mut gpsl = GPSL::new(
Source::new(String::default()),
Some(functions),
Some(HashMap::new()),
Some(HashMap::new()),