forked from M-Labs/nac3
remove bigints
This commit is contained in:
parent
ec986dfdf3
commit
80d3ab1b0f
35
Cargo.lock
generated
35
Cargo.lock
generated
@ -497,7 +497,6 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"fxhash",
|
"fxhash",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"num-bigint 0.4.3",
|
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"string-interner",
|
"string-interner",
|
||||||
]
|
]
|
||||||
@ -512,7 +511,6 @@ dependencies = [
|
|||||||
"insta",
|
"insta",
|
||||||
"itertools",
|
"itertools",
|
||||||
"nac3parser",
|
"nac3parser",
|
||||||
"num-bigint 0.3.3",
|
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"rayon",
|
"rayon",
|
||||||
@ -529,7 +527,6 @@ dependencies = [
|
|||||||
"lalrpop-util",
|
"lalrpop-util",
|
||||||
"log",
|
"log",
|
||||||
"nac3ast",
|
"nac3ast",
|
||||||
"num-bigint 0.4.3",
|
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"phf",
|
"phf",
|
||||||
"unic-emoji-char",
|
"unic-emoji-char",
|
||||||
@ -553,38 +550,6 @@ version = "1.0.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
|
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-bigint"
|
|
||||||
version = "0.3.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5f6f7833f2cbf2360a6cfd58cd41a53aa7a90bd4c202f5b1c7dd2ed73c57b2c3"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
"num-integer",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-bigint"
|
|
||||||
version = "0.4.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
"num-integer",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-integer"
|
|
||||||
version = "0.1.44"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-traits"
|
name = "num-traits"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
|
@ -10,7 +10,6 @@ constant-optimization = ["fold"]
|
|||||||
fold = []
|
fold = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num-bigint = "0.4.0"
|
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
parking_lot = "0.11.1"
|
parking_lot = "0.11.1"
|
||||||
string-interner = "0.13.0"
|
string-interner = "0.13.0"
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
use num_bigint::BigInt;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Constant {
|
pub enum Constant {
|
||||||
None,
|
None,
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
Str(String),
|
Str(String),
|
||||||
Bytes(Vec<u8>),
|
Bytes(Vec<u8>),
|
||||||
Int(BigInt),
|
Int(Option<i64>),
|
||||||
Tuple(Vec<Constant>),
|
Tuple(Vec<Constant>),
|
||||||
Float(f64),
|
Float(f64),
|
||||||
Complex { real: f64, imag: f64 },
|
Complex { real: f64, imag: f64 },
|
||||||
@ -28,9 +26,14 @@ impl From<bool> for Constant {
|
|||||||
Self::Bool(b)
|
Self::Bool(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<BigInt> for Constant {
|
impl From<i32> for Constant {
|
||||||
fn from(i: BigInt) -> Constant {
|
fn from(i: i32) -> Constant {
|
||||||
Self::Int(i)
|
Self::Int(Some(i as i64))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<i64> for Constant {
|
||||||
|
fn from(i: i64) -> Constant {
|
||||||
|
Self::Int(Some(i))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +139,7 @@ mod tests {
|
|||||||
location,
|
location,
|
||||||
custom,
|
custom,
|
||||||
node: ExprKind::Constant {
|
node: ExprKind::Constant {
|
||||||
value: BigInt::from(1).into(),
|
value: 1.into(),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -144,7 +147,7 @@ mod tests {
|
|||||||
location,
|
location,
|
||||||
custom,
|
custom,
|
||||||
node: ExprKind::Constant {
|
node: ExprKind::Constant {
|
||||||
value: BigInt::from(2).into(),
|
value: 2.into(),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -158,7 +161,7 @@ mod tests {
|
|||||||
location,
|
location,
|
||||||
custom,
|
custom,
|
||||||
node: ExprKind::Constant {
|
node: ExprKind::Constant {
|
||||||
value: BigInt::from(3).into(),
|
value: 3.into(),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -166,7 +169,7 @@ mod tests {
|
|||||||
location,
|
location,
|
||||||
custom,
|
custom,
|
||||||
node: ExprKind::Constant {
|
node: ExprKind::Constant {
|
||||||
value: BigInt::from(4).into(),
|
value: 4.into(),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -174,7 +177,7 @@ mod tests {
|
|||||||
location,
|
location,
|
||||||
custom,
|
custom,
|
||||||
node: ExprKind::Constant {
|
node: ExprKind::Constant {
|
||||||
value: BigInt::from(5).into(),
|
value: 5.into(),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -194,12 +197,12 @@ mod tests {
|
|||||||
custom,
|
custom,
|
||||||
node: ExprKind::Constant {
|
node: ExprKind::Constant {
|
||||||
value: Constant::Tuple(vec![
|
value: Constant::Tuple(vec![
|
||||||
BigInt::from(1).into(),
|
1.into(),
|
||||||
BigInt::from(2).into(),
|
2.into(),
|
||||||
Constant::Tuple(vec![
|
Constant::Tuple(vec![
|
||||||
BigInt::from(3).into(),
|
3.into(),
|
||||||
BigInt::from(4).into(),
|
4.into(),
|
||||||
BigInt::from(5).into(),
|
5.into(),
|
||||||
])
|
])
|
||||||
]),
|
]),
|
||||||
kind: None
|
kind: None
|
||||||
|
@ -5,7 +5,6 @@ authors = ["M-Labs"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num-bigint = "0.3"
|
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
itertools = "0.10.1"
|
itertools = "0.10.1"
|
||||||
crossbeam = "0.8.1"
|
crossbeam = "0.8.1"
|
||||||
|
@ -123,7 +123,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
|||||||
let ty = self.ctx.bool_type();
|
let ty = self.ctx.bool_type();
|
||||||
ty.const_int(if *v { 1 } else { 0 }, false).into()
|
ty.const_int(if *v { 1 } else { 0 }, false).into()
|
||||||
}
|
}
|
||||||
Constant::Int(v) => {
|
Constant::Int(Some(val)) => {
|
||||||
let ty = if self.unifier.unioned(ty, self.primitives.int32) {
|
let ty = if self.unifier.unioned(ty, self.primitives.int32) {
|
||||||
self.ctx.i32_type()
|
self.ctx.i32_type()
|
||||||
} else if self.unifier.unioned(ty, self.primitives.int64) {
|
} else if self.unifier.unioned(ty, self.primitives.int64) {
|
||||||
@ -131,8 +131,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
|||||||
} else {
|
} else {
|
||||||
unreachable!();
|
unreachable!();
|
||||||
};
|
};
|
||||||
let val: i64 = v.try_into().unwrap();
|
ty.const_int(*val as u64, false).into()
|
||||||
ty.const_int(val as u64, false).into()
|
|
||||||
}
|
}
|
||||||
Constant::Float(v) => {
|
Constant::Float(v) => {
|
||||||
assert!(self.unifier.unioned(ty, self.primitives.float));
|
assert!(self.unifier.unioned(ty, self.primitives.float));
|
||||||
|
@ -412,7 +412,9 @@ pub fn parse_parameter_default_value(default: &ast::Expr, resolver: &(dyn Symbol
|
|||||||
fn handle_constant(val: &Constant, loc: &Location) -> Result<SymbolValue, String> {
|
fn handle_constant(val: &Constant, loc: &Location) -> Result<SymbolValue, String> {
|
||||||
match val {
|
match val {
|
||||||
Constant::Int(v) => {
|
Constant::Int(v) => {
|
||||||
if let Ok(v) = v.try_into() {
|
match v {
|
||||||
|
Some(v) => {
|
||||||
|
if let Ok(v) = (*v).try_into() {
|
||||||
Ok(SymbolValue::I32(v))
|
Ok(SymbolValue::I32(v))
|
||||||
} else {
|
} else {
|
||||||
Err(format!(
|
Err(format!(
|
||||||
@ -420,6 +422,14 @@ pub fn parse_parameter_default_value(default: &ast::Expr, resolver: &(dyn Symbol
|
|||||||
loc
|
loc
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
Err(format!(
|
||||||
|
"integer value out of range at {}",
|
||||||
|
loc
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Constant::Float(v) => Ok(SymbolValue::Double(*v)),
|
Constant::Float(v) => Ok(SymbolValue::Double(*v)),
|
||||||
Constant::Bool(v) => Ok(SymbolValue::Bool(*v)),
|
Constant::Bool(v) => Ok(SymbolValue::Bool(*v)),
|
||||||
@ -439,8 +449,8 @@ pub fn parse_parameter_default_value(default: &ast::Expr, resolver: &(dyn Symbol
|
|||||||
} => {
|
} => {
|
||||||
if args.len() == 1 {
|
if args.len() == 1 {
|
||||||
match &args[0].node {
|
match &args[0].node {
|
||||||
ast::ExprKind::Constant { value: Constant::Int(v), .. } =>
|
ast::ExprKind::Constant { value: Constant::Int(Some(v)), .. } =>
|
||||||
Ok(SymbolValue::I64(v.try_into().unwrap())),
|
Ok(SymbolValue::I64(*v)),
|
||||||
_ => Err(format!("only allow constant integer here at {}", default.location))
|
_ => Err(format!("only allow constant integer here at {}", default.location))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -680,11 +680,8 @@ impl<'a> Inferencer<'a> {
|
|||||||
if let ExprKind::Constant { value: ast::Constant::Int(val), kind } =
|
if let ExprKind::Constant { value: ast::Constant::Int(val), kind } =
|
||||||
&args[0].node
|
&args[0].node
|
||||||
{
|
{
|
||||||
let int64: Result<i64, _> = val.try_into();
|
let custom = Some(self.primitives.int64);
|
||||||
let custom;
|
if val.is_none() {
|
||||||
if int64.is_ok() {
|
|
||||||
custom = Some(self.primitives.int64);
|
|
||||||
} else {
|
|
||||||
return Err("Integer out of bound".into());
|
return Err("Integer out of bound".into());
|
||||||
}
|
}
|
||||||
return Ok(Located {
|
return Ok(Located {
|
||||||
@ -777,13 +774,18 @@ impl<'a> Inferencer<'a> {
|
|||||||
match constant {
|
match constant {
|
||||||
ast::Constant::Bool(_) => Ok(self.primitives.bool),
|
ast::Constant::Bool(_) => Ok(self.primitives.bool),
|
||||||
ast::Constant::Int(val) => {
|
ast::Constant::Int(val) => {
|
||||||
let int32: Result<i32, _> = val.try_into();
|
match val {
|
||||||
// int64 would be handled separately in functions
|
Some(val) => {
|
||||||
|
let int32: Result<i32, _> = (*val).try_into();
|
||||||
|
// int64 is handled separately in functions
|
||||||
if int32.is_ok() {
|
if int32.is_ok() {
|
||||||
Ok(self.primitives.int32)
|
Ok(self.primitives.int32)
|
||||||
} else {
|
} else {
|
||||||
Err("Integer out of bound".into())
|
Err("Integer out of bound".into())
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
None => Err("Integer out of bound".into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ast::Constant::Float(_) => Ok(self.primitives.float),
|
ast::Constant::Float(_) => Ok(self.primitives.float),
|
||||||
ast::Constant::Tuple(vals) => {
|
ast::Constant::Tuple(vals) => {
|
||||||
@ -907,7 +909,11 @@ impl<'a> Inferencer<'a> {
|
|||||||
}
|
}
|
||||||
ast::ExprKind::Constant { value: ast::Constant::Int(val), .. } => {
|
ast::ExprKind::Constant { value: ast::Constant::Int(val), .. } => {
|
||||||
// the index is a constant, so value can be a sequence.
|
// the index is a constant, so value can be a sequence.
|
||||||
let ind: i32 = val.try_into().map_err(|_| "Index must be int32".to_string())?;
|
let ind: Option<i32> = match val {
|
||||||
|
Some(val) => (*val).try_into().ok(),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
let ind = ind.ok_or_else(|| "Index must be int32".to_string())?;
|
||||||
let map = once((ind, ty)).collect();
|
let map = once((ind, ty)).collect();
|
||||||
let seq = self.unifier.add_sequence(map);
|
let seq = self.unifier.add_sequence(map);
|
||||||
self.constrain(value.custom.unwrap(), seq, &value.location)?;
|
self.constrain(value.custom.unwrap(), seq, &value.location)?;
|
||||||
|
@ -14,7 +14,6 @@ lalrpop = "0.19.6"
|
|||||||
nac3ast = { path = "../nac3ast" }
|
nac3ast = { path = "../nac3ast" }
|
||||||
lalrpop-util = "0.19.6"
|
lalrpop-util = "0.19.6"
|
||||||
log = "0.4.1"
|
log = "0.4.1"
|
||||||
num-bigint = "0.4.0"
|
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
unic-emoji-char = "0.9"
|
unic-emoji-char = "0.9"
|
||||||
unic-ucd-ident = "0.9"
|
unic-ucd-ident = "0.9"
|
||||||
|
@ -5,12 +5,11 @@
|
|||||||
pub use super::token::Tok;
|
pub use super::token::Tok;
|
||||||
use crate::ast::Location;
|
use crate::ast::Location;
|
||||||
use crate::error::{LexicalError, LexicalErrorType};
|
use crate::error::{LexicalError, LexicalErrorType};
|
||||||
use num_bigint::BigInt;
|
|
||||||
use num_traits::identities::Zero;
|
use num_traits::identities::Zero;
|
||||||
use num_traits::Num;
|
|
||||||
use std::char;
|
use std::char;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use std::num::IntErrorKind;
|
||||||
use unic_emoji_char::is_emoji_presentation;
|
use unic_emoji_char::is_emoji_presentation;
|
||||||
use unic_ucd_ident::{is_xid_continue, is_xid_start};
|
use unic_ucd_ident::{is_xid_continue, is_xid_start};
|
||||||
|
|
||||||
@ -287,10 +286,18 @@ where
|
|||||||
fn lex_number_radix(&mut self, start_pos: Location, radix: u32) -> LexResult {
|
fn lex_number_radix(&mut self, start_pos: Location, radix: u32) -> LexResult {
|
||||||
let value_text = self.radix_run(radix);
|
let value_text = self.radix_run(radix);
|
||||||
let end_pos = self.get_pos();
|
let end_pos = self.get_pos();
|
||||||
let value = BigInt::from_str_radix(&value_text, radix).map_err(|e| LexicalError {
|
let value = match i64::from_str_radix(&value_text, radix) {
|
||||||
|
Ok(value) => Some(value),
|
||||||
|
Err(e) => {
|
||||||
|
match e.kind() {
|
||||||
|
IntErrorKind::PosOverflow | IntErrorKind::NegOverflow => None,
|
||||||
|
_ => return Err(LexicalError {
|
||||||
error: LexicalErrorType::OtherError(format!("{:?}", e)),
|
error: LexicalErrorType::OtherError(format!("{:?}", e)),
|
||||||
location: start_pos,
|
location: start_pos,
|
||||||
})?;
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
Ok((start_pos, Tok::Int { value }, end_pos))
|
Ok((start_pos, Tok::Int { value }, end_pos))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,8 +360,14 @@ where
|
|||||||
Ok((start_pos, Tok::Complex { real: 0.0, imag }, end_pos))
|
Ok((start_pos, Tok::Complex { real: 0.0, imag }, end_pos))
|
||||||
} else {
|
} else {
|
||||||
let end_pos = self.get_pos();
|
let end_pos = self.get_pos();
|
||||||
let value = value_text.parse::<BigInt>().unwrap();
|
// assumption: value_text contains a valid integer.
|
||||||
if start_is_zero && !value.is_zero() {
|
// parse should only fail because of overflow.
|
||||||
|
let value = value_text.parse::<i64>().ok();
|
||||||
|
let nonzero = match value {
|
||||||
|
Some(value) => !value.is_zero(),
|
||||||
|
None => true
|
||||||
|
};
|
||||||
|
if start_is_zero && nonzero {
|
||||||
return Err(LexicalError {
|
return Err(LexicalError {
|
||||||
error: LexicalErrorType::OtherError("Invalid Token".to_owned()),
|
error: LexicalErrorType::OtherError("Invalid Token".to_owned()),
|
||||||
location: self.get_pos(),
|
location: self.get_pos(),
|
||||||
@ -1321,7 +1334,6 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{make_tokenizer, NewlineHandler, Tok};
|
use super::{make_tokenizer, NewlineHandler, Tok};
|
||||||
use num_bigint::BigInt;
|
|
||||||
|
|
||||||
const WINDOWS_EOL: &str = "\r\n";
|
const WINDOWS_EOL: &str = "\r\n";
|
||||||
const MAC_EOL: &str = "\r";
|
const MAC_EOL: &str = "\r";
|
||||||
@ -1449,16 +1461,16 @@ class Foo(A, B):
|
|||||||
tokens,
|
tokens,
|
||||||
vec![
|
vec![
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(47),
|
value: Some(47i64),
|
||||||
},
|
},
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(13),
|
value: Some(13i64),
|
||||||
},
|
},
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(0),
|
value: Some(0i64),
|
||||||
},
|
},
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(123),
|
value: Some(123i64),
|
||||||
},
|
},
|
||||||
Tok::Float { value: 0.2 },
|
Tok::Float { value: 0.2 },
|
||||||
Tok::Complex {
|
Tok::Complex {
|
||||||
@ -1481,7 +1493,7 @@ class Foo(A, B):
|
|||||||
fn $name() {
|
fn $name() {
|
||||||
let source = format!(r"99232 # {}", $eol);
|
let source = format!(r"99232 # {}", $eol);
|
||||||
let tokens = lex_source(&source);
|
let tokens = lex_source(&source);
|
||||||
assert_eq!(tokens, vec![Tok::Int { value: BigInt::from(99232) }, Tok::Newline]);
|
assert_eq!(tokens, vec![Tok::Int { value: Some(99232i64) }, Tok::Newline]);
|
||||||
}
|
}
|
||||||
)*
|
)*
|
||||||
}
|
}
|
||||||
@ -1504,9 +1516,9 @@ class Foo(A, B):
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
tokens,
|
tokens,
|
||||||
vec![
|
vec![
|
||||||
Tok::Int { value: BigInt::from(123) },
|
Tok::Int { value: Some(123i64) },
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Int { value: BigInt::from(456) },
|
Tok::Int { value: Some(456i64) },
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@ -1533,15 +1545,15 @@ class Foo(A, B):
|
|||||||
},
|
},
|
||||||
Tok::Equal,
|
Tok::Equal,
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(99)
|
value: Some(99i64)
|
||||||
},
|
},
|
||||||
Tok::Plus,
|
Tok::Plus,
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(2)
|
value: Some(2i64)
|
||||||
},
|
},
|
||||||
Tok::Minus,
|
Tok::Minus,
|
||||||
Tok::Int {
|
Tok::Int {
|
||||||
value: BigInt::from(0)
|
value: Some(0i64)
|
||||||
},
|
},
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
]
|
]
|
||||||
@ -1568,7 +1580,7 @@ class Foo(A, B):
|
|||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Indent,
|
Tok::Indent,
|
||||||
Tok::Return,
|
Tok::Return,
|
||||||
Tok::Int { value: BigInt::from(99) },
|
Tok::Int { value: Some(99i64) },
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Dedent,
|
Tok::Dedent,
|
||||||
]
|
]
|
||||||
@ -1611,7 +1623,7 @@ class Foo(A, B):
|
|||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Indent,
|
Tok::Indent,
|
||||||
Tok::Return,
|
Tok::Return,
|
||||||
Tok::Int { value: BigInt::from(99) },
|
Tok::Int { value: Some(99i64) },
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Dedent,
|
Tok::Dedent,
|
||||||
Tok::Dedent,
|
Tok::Dedent,
|
||||||
@ -1649,7 +1661,7 @@ class Foo(A, B):
|
|||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Indent,
|
Tok::Indent,
|
||||||
Tok::Return,
|
Tok::Return,
|
||||||
Tok::Int { value: BigInt::from(99) },
|
Tok::Int { value: Some(99i64) },
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
Tok::Dedent,
|
Tok::Dedent,
|
||||||
Tok::Dedent,
|
Tok::Dedent,
|
||||||
@ -1687,9 +1699,9 @@ class Foo(A, B):
|
|||||||
},
|
},
|
||||||
Tok::Equal,
|
Tok::Equal,
|
||||||
Tok::Lsqb,
|
Tok::Lsqb,
|
||||||
Tok::Int { value: BigInt::from(1) },
|
Tok::Int { value: Some(1i64) },
|
||||||
Tok::Comma,
|
Tok::Comma,
|
||||||
Tok::Int { value: BigInt::from(2) },
|
Tok::Int { value: Some(2i64) },
|
||||||
Tok::Rsqb,
|
Tok::Rsqb,
|
||||||
Tok::Newline,
|
Tok::Newline,
|
||||||
]
|
]
|
||||||
|
@ -31,7 +31,6 @@ pub fn parse_program(source: &str) -> Result<ast::Suite, ParseError> {
|
|||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```
|
/// ```
|
||||||
/// extern crate num_bigint;
|
|
||||||
/// use nac3parser::{parser, ast};
|
/// use nac3parser::{parser, ast};
|
||||||
/// let expr = parser::parse_expression("1 + 2").unwrap();
|
/// let expr = parser::parse_expression("1 + 2").unwrap();
|
||||||
///
|
///
|
||||||
|
@ -14,7 +14,6 @@ use crate::lexer;
|
|||||||
use crate::config_comment_helper::*;
|
use crate::config_comment_helper::*;
|
||||||
|
|
||||||
use lalrpop_util::ParseError;
|
use lalrpop_util::ParseError;
|
||||||
use num_bigint::BigInt;
|
|
||||||
|
|
||||||
grammar;
|
grammar;
|
||||||
|
|
||||||
@ -920,7 +919,7 @@ Factor: ast::Expr = {
|
|||||||
match (&op, &e.node) {
|
match (&op, &e.node) {
|
||||||
(ast::Unaryop::USub, ast::ExprKind::Constant { value: Constant::Int(val), kind }) => {
|
(ast::Unaryop::USub, ast::ExprKind::Constant { value: Constant::Int(val), kind }) => {
|
||||||
ast::ExprKind::Constant {
|
ast::ExprKind::Constant {
|
||||||
value: Constant::Int(-val),
|
value: if let Some(val) = val { Constant::Int(Some(-val)) } else { Constant::Int(None) },
|
||||||
kind: kind.clone()
|
kind: kind.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1362,7 +1361,7 @@ extern {
|
|||||||
"True" => lexer::Tok::True,
|
"True" => lexer::Tok::True,
|
||||||
"False" => lexer::Tok::False,
|
"False" => lexer::Tok::False,
|
||||||
"None" => lexer::Tok::None,
|
"None" => lexer::Tok::None,
|
||||||
int => lexer::Tok::Int { value: <BigInt> },
|
int => lexer::Tok::Int { value: <Option<i64>> },
|
||||||
float => lexer::Tok::Float { value: <f64> },
|
float => lexer::Tok::Float { value: <f64> },
|
||||||
complex => lexer::Tok::Complex { real: <f64>, imag: <f64> },
|
complex => lexer::Tok::Complex { real: <f64>, imag: <f64> },
|
||||||
string => lexer::Tok::String { value: <String>, is_fstring: <bool> },
|
string => lexer::Tok::String { value: <String>, is_fstring: <bool> },
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: parser/src/fstring.rs
|
source: nac3parser/src/fstring.rs
|
||||||
|
assertion_line: 382
|
||||||
expression: parse_ast
|
expression: parse_ast
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -25,8 +26,10 @@ Located {
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
42,
|
42,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -42,8 +45,10 @@ Located {
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
42,
|
42,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: parser/src/fstring.rs
|
source: nac3parser/src/fstring.rs
|
||||||
|
assertion_line: 375
|
||||||
expression: parse_ast
|
expression: parse_ast
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -25,8 +26,10 @@ Located {
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -42,8 +45,10 @@ Located {
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 218
|
||||||
expression: parse_program(&source).unwrap()
|
expression: parse_program(&source).unwrap()
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -172,8 +173,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
3,
|
3,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -219,8 +222,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
3,
|
3,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -262,8 +267,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
3,
|
3,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -337,8 +344,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
3,
|
3,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 186
|
||||||
expression: parse_program(&source).unwrap()
|
expression: parse_program(&source).unwrap()
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -74,8 +75,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -177,8 +180,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -220,8 +225,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -234,8 +241,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -263,8 +272,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -284,8 +295,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
3,
|
3,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 164
|
||||||
expression: parse_ast
|
expression: parse_ast
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -125,8 +126,10 @@ Located {
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
5,
|
5,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -163,8 +166,10 @@ Located {
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
10,
|
10,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 118
|
||||||
expression: parse_ast
|
expression: parse_ast
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -19,8 +20,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -40,8 +43,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
10,
|
10,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -65,8 +70,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -86,8 +93,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
20,
|
20,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -111,8 +120,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
30,
|
30,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 111
|
||||||
expression: parse_ast
|
expression: parse_ast
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -63,8 +64,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 104
|
||||||
expression: parse_ast
|
expression: parse_ast
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -51,8 +52,10 @@ expression: parse_ast
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
2,
|
2,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 132
|
||||||
expression: parse_program(&source).unwrap()
|
expression: parse_program(&source).unwrap()
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -63,8 +64,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
4,
|
4,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -76,8 +79,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
5,
|
5,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
---
|
---
|
||||||
source: nac3parser/src/parser.rs
|
source: nac3parser/src/parser.rs
|
||||||
|
assertion_line: 199
|
||||||
expression: parse_program(&source).unwrap()
|
expression: parse_program(&source).unwrap()
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -51,8 +52,10 @@ expression: parse_program(&source).unwrap()
|
|||||||
custom: (),
|
custom: (),
|
||||||
node: Constant {
|
node: Constant {
|
||||||
value: Int(
|
value: Int(
|
||||||
|
Some(
|
||||||
3,
|
3,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
kind: None,
|
kind: None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
//! Different token definitions.
|
//! Different token definitions.
|
||||||
//! Loosely based on token.h from CPython source:
|
//! Loosely based on token.h from CPython source:
|
||||||
use num_bigint::BigInt;
|
|
||||||
use std::fmt::{self, Write};
|
use std::fmt::{self, Write};
|
||||||
use crate::ast;
|
use crate::ast;
|
||||||
|
|
||||||
@ -8,7 +7,7 @@ use crate::ast;
|
|||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum Tok {
|
pub enum Tok {
|
||||||
Name { name: ast::StrRef },
|
Name { name: ast::StrRef },
|
||||||
Int { value: BigInt },
|
Int { value: Option<i64> },
|
||||||
Float { value: f64 },
|
Float { value: f64 },
|
||||||
Complex { real: f64, imag: f64 },
|
Complex { real: f64, imag: f64 },
|
||||||
String { value: String, is_fstring: bool },
|
String { value: String, is_fstring: bool },
|
||||||
@ -113,7 +112,7 @@ impl fmt::Display for Tok {
|
|||||||
use Tok::*;
|
use Tok::*;
|
||||||
match self {
|
match self {
|
||||||
Name { name } => write!(f, "'{}'", ast::get_str_from_ref(&ast::get_str_ref_lock(), *name)),
|
Name { name } => write!(f, "'{}'", ast::get_str_from_ref(&ast::get_str_ref_lock(), *name)),
|
||||||
Int { value } => write!(f, "'{}'", value),
|
Int { value } => if let Some(value) = value { write!(f, "'{}'", value) } else { write!(f, "'#OFL#'") },
|
||||||
Float { value } => write!(f, "'{}'", value),
|
Float { value } => write!(f, "'{}'", value),
|
||||||
Complex { real, imag } => write!(f, "{}j{}", real, imag),
|
Complex { real, imag } => write!(f, "{}j{}", real, imag),
|
||||||
String { value, is_fstring } => {
|
String { value, is_fstring } => {
|
||||||
|
Loading…
Reference in New Issue
Block a user