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)] #[clap(short, long, takes_value = false)]
pub debug: bool, pub debug: bool,
#[clap(short, long, takes_value = false)]
pub compile: bool,
} }

View File

@ -1,14 +1,16 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs, fs::{self, File},
io::{BufRead, BufReader, Write}, io::{BufRead, BufReader, Read, Write},
net::TcpStream, net::TcpStream,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use flate2::{read::ZlibDecoder, write::ZlibEncoder, Compression};
use crate::{ use crate::{
args::Args, args::Args,
config::Config, config::{Config, ConfigFile},
elliptic_curve::encryption::Encryption, elliptic_curve::encryption::Encryption,
gpsl::{ gpsl::{
self, self,
@ -21,8 +23,26 @@ use crate::{
}; };
pub fn start_client(args: Args) { pub fn start_client(args: Args) {
let mut source = let file = args.file.clone().unwrap();
Source::new(fs::read_to_string(&(args.file.unwrap())).expect("Cannot read file.")); let file_name = {
let file_name = file.file_name();
file_name.unwrap().to_string_lossy()
};
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(); let mut tokenizer = Tokenizer::new();
tokenizer.tokenize(&mut source).unwrap(); tokenizer.tokenize(&mut source).unwrap();
@ -32,7 +52,20 @@ pub fn start_client(args: Args) {
local_vars: HashMap::new(), local_vars: HashMap::new(),
}; };
let functions = parser.functions().unwrap(); 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(); let mut server_functions: HashMap<String, HashMap<String, Box<Node>>> = HashMap::new();
for function in functions.clone() { for function in functions.clone() {
@ -105,7 +138,6 @@ pub fn start_client(args: Args) {
let config = Config::read_or_create(); let config = Config::read_or_create();
let mut gpsl = GPSL::new( let mut gpsl = GPSL::new(
source,
Some(functions), Some(functions),
Some(server_functions), Some(server_functions),
Some(servers), Some(servers),

View File

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

View File

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