文字列の結合機能

This commit is contained in:
Masato Imai
2022-07-25 14:08:36 +09:00
parent 4842bf561b
commit 78f923360f
4 changed files with 53 additions and 26 deletions

View File

@ -1,15 +1,12 @@
#[server(ip = "172.25.5.189:8080")] #[server(ip = "172.25.5.189:8080")]
fn encrypt_add(a: eep, b: eep) { fn encrypt_add(a: eep, b: eep) {
print("a: "); print("a: ")
println(a); println(a)
print("b: "); print("b: ")
println(b); println(b)
return a + b; return a + b
} }
fn main() { fn main() {
print("Input a: ") print("Input a: ")
let a = to_u512(read_line()) let a = to_u512(read_line())
@ -17,14 +14,12 @@ fn main() {
print("Input b: ") print("Input b: ")
let b = to_u512(read_line()) let b = to_u512(read_line())
println("a: " + a);
let enc_a = encrypt(a) let enc_a = encrypt(a)
let enc_b = encrypt(b) let enc_b = encrypt(b)
let enc_res = encrypt_add(enc_a, enc_b) let enc_res = encrypt_add(enc_a, enc_b)
println(decrypt(enc_res)) println(decrypt(enc_res))
for (let i=0 i<10 i+=1) {
println(i)
}
} }

View File

@ -36,7 +36,7 @@ add: mul (ADD mul | SUB mul | SUB_ASSIGNMENT mul | ADD_ASSIGNMENT mul)* ;
mul: unary (MUL unary | DIV unary | DIV_ASSIGNMENT unary | MUL_ASSIGNMENT unary)* ; mul: unary (MUL unary | DIV unary | DIV_ASSIGNMENT unary | MUL_ASSIGNMENT unary)* ;
primary: LPAREN expr RPAREN | function_call | TEXT | NUM ; primary: LPAREN expr RPAREN | function_call | TEXT | NUM ;
function_call: IDENT LPAREN (unary COMMA?)* RPAREN ; function_call: IDENT LPAREN (stmt COMMA?)* RPAREN ;
unary: ADD primary unary: ADD primary
| SUB primary | SUB primary

View File

@ -480,7 +480,7 @@ impl Parser {
if self.tokenizer.consume(String::from("(")) { if self.tokenizer.consume(String::from("(")) {
let mut args: Vec<Box<Node>> = vec![]; let mut args: Vec<Box<Node>> = vec![];
while self.tokenizer.current_token().str != ")" { while self.tokenizer.current_token().str != ")" {
args.push(self.unary()?); args.push(self.stmt()?);
self.tokenizer.consume(String::from(",")); self.tokenizer.consume(String::from(","));
} }

View File

@ -301,20 +301,52 @@ impl GPSL {
if let Some(lhs) = lhs.clone() { if let Some(lhs) = lhs.clone() {
if let Some(rhs) = rhs { if let Some(rhs) = rhs {
match kind { match kind {
NodeKind::ADD => match GPSL::extract_number(lhs.clone()) { NodeKind::ADD => match lhs.clone() {
Ok(lhs) => match GPSL::extract_number(rhs) { Variable::Number { value: lhs } => match rhs {
Ok(rhs) => Ok(Some(Variable::Number { value: lhs + rhs })), Variable::Number { value: rhs } => {
Err(err) => Err(err), Ok(Some(Variable::Number { value: lhs + rhs }))
}
Variable::Text { value: rhs } => Ok(Some(Variable::Text {
value: format!("{}{}", lhs, rhs),
})),
_ => Err("Cannot add non-number to number.".to_string()),
}, },
Err(_) => match GPSL::extract_eep(lhs) { Variable::PureEncrypted { value: lhs } => match rhs {
Ok(lhs) => match GPSL::extract_eep(rhs) { Variable::PureEncrypted { value: rhs } => {
Ok(rhs) => { Ok(Some(Variable::PureEncrypted { value: lhs + rhs }))
Ok(Some(Variable::PureEncrypted { value: lhs + rhs })) }
} Variable::Text { value: rhs } => Ok(Some(Variable::Text {
Err(err) => Err(err), value: format!("{}{}", lhs, rhs),
}, })),
Err(err) => Err(err), _ => Err("Cannot add non-number to number.".to_string()),
}, },
Variable::U512 { value: lhs } => match rhs {
Variable::U512 { value: rhs } => {
Ok(Some(Variable::U512 { value: lhs + rhs }))
}
Variable::Text { value: rhs } => Ok(Some(Variable::Text {
value: lhs.to_string() + &rhs,
})),
_ => Err("Cannot add non-number to number.".to_string()),
},
Variable::Text { value: lhs } => match rhs {
Variable::Text { value: rhs } => Ok(Some(Variable::Text {
value: format!("{}{}", lhs, rhs),
})),
Variable::Number { value: rhs } => Ok(Some(Variable::Text {
value: format!("{}{}", lhs, rhs),
})),
Variable::PureEncrypted { value: rhs } => {
Ok(Some(Variable::Text {
value: format!("{}{}", lhs, rhs),
}))
}
Variable::U512 { value: rhs } => Ok(Some(Variable::Text {
value: format!("{}{}", lhs, rhs),
})),
_ => Err("Cannot add non-number to number.".to_string()),
},
_ => Err("Cannot add non-number.".to_string()),
}, },
NodeKind::DIV => match GPSL::extract_number(lhs) { NodeKind::DIV => match GPSL::extract_number(lhs) {
Ok(lhs) => match GPSL::extract_number(rhs) { Ok(lhs) => match GPSL::extract_number(rhs) {