Rename (Un)TypedExpr.Int -> (Un)TypedExpr.UInt

We do not actually every parse negative values in there, as a negative value is a combination of a 'Negate' and 'UInt' expression.
  However, for patterns and constant, it'll be simpler to parse whole Int values as there's no ambiguity with arithmetic operations
  there. To avoid confusion of having some 'Int' constructors containing only non-negative values, and some being on the whole range,
  I've renamed the constructor to 'UInt' to make this more obvious.
This commit is contained in:
KtorZ 2023-07-06 10:21:21 +02:00 committed by Lucas
parent 5a4a2faa4d
commit 549cf22cdd
30 changed files with 80 additions and 79 deletions

View File

@ -15,7 +15,7 @@ use crate::{
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum TypedExpr { pub enum TypedExpr {
Int { UInt {
location: Span, location: Span,
tipo: Arc<Type>, tipo: Arc<Type>,
value: String, value: String,
@ -170,7 +170,7 @@ impl TypedExpr {
Self::Var { constructor, .. } => constructor.tipo.clone(), Self::Var { constructor, .. } => constructor.tipo.clone(),
Self::Trace { then, .. } => then.tipo(), Self::Trace { then, .. } => then.tipo(),
Self::Fn { tipo, .. } Self::Fn { tipo, .. }
| Self::Int { tipo, .. } | Self::UInt { tipo, .. }
| Self::ErrorTerm { tipo, .. } | Self::ErrorTerm { tipo, .. }
| Self::When { tipo, .. } | Self::When { tipo, .. }
| Self::List { tipo, .. } | Self::List { tipo, .. }
@ -195,7 +195,7 @@ impl TypedExpr {
pub fn is_literal(&self) -> bool { pub fn is_literal(&self) -> bool {
matches!( matches!(
self, self,
Self::Int { .. } Self::UInt { .. }
| Self::List { .. } | Self::List { .. }
| Self::Tuple { .. } | Self::Tuple { .. }
| Self::String { .. } | Self::String { .. }
@ -211,7 +211,7 @@ impl TypedExpr {
pub fn definition_location(&self) -> Option<DefinitionLocation<'_>> { pub fn definition_location(&self) -> Option<DefinitionLocation<'_>> {
match self { match self {
TypedExpr::Fn { .. } TypedExpr::Fn { .. }
| TypedExpr::Int { .. } | TypedExpr::UInt { .. }
| TypedExpr::Trace { .. } | TypedExpr::Trace { .. }
| TypedExpr::List { .. } | TypedExpr::List { .. }
| TypedExpr::Call { .. } | TypedExpr::Call { .. }
@ -251,7 +251,7 @@ impl TypedExpr {
pub fn type_defining_location(&self) -> Span { pub fn type_defining_location(&self) -> Span {
match self { match self {
Self::Fn { location, .. } Self::Fn { location, .. }
| Self::Int { location, .. } | Self::UInt { location, .. }
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::Trace { location, .. } | Self::Trace { location, .. }
| Self::ErrorTerm { location, .. } | Self::ErrorTerm { location, .. }
@ -286,7 +286,7 @@ impl TypedExpr {
pub fn location(&self) -> Span { pub fn location(&self) -> Span {
match self { match self {
Self::Fn { location, .. } Self::Fn { location, .. }
| Self::Int { location, .. } | Self::UInt { location, .. }
| Self::Trace { location, .. } | Self::Trace { location, .. }
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::ErrorTerm { location, .. } | Self::ErrorTerm { location, .. }
@ -319,7 +319,7 @@ impl TypedExpr {
match self { match self {
TypedExpr::ErrorTerm { .. } TypedExpr::ErrorTerm { .. }
| TypedExpr::Var { .. } | TypedExpr::Var { .. }
| TypedExpr::Int { .. } | TypedExpr::UInt { .. }
| TypedExpr::String { .. } | TypedExpr::String { .. }
| TypedExpr::ByteArray { .. } | TypedExpr::ByteArray { .. }
| TypedExpr::ModuleSelect { .. } => Some(self), | TypedExpr::ModuleSelect { .. } => Some(self),
@ -409,7 +409,7 @@ pub enum FnStyle {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum UntypedExpr { pub enum UntypedExpr {
Int { UInt {
location: Span, location: Span,
value: String, value: String,
base: Base, base: Base,
@ -619,7 +619,7 @@ impl UntypedExpr {
Self::TraceIfFalse { location, .. } Self::TraceIfFalse { location, .. }
| Self::Fn { location, .. } | Self::Fn { location, .. }
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::Int { location, .. } | Self::UInt { location, .. }
| Self::ErrorTerm { location, .. } | Self::ErrorTerm { location, .. }
| Self::When { location, .. } | Self::When { location, .. }
| Self::Call { location, .. } | Self::Call { location, .. }
@ -669,7 +669,7 @@ impl UntypedExpr {
pub fn is_simple_constant(&self) -> bool { pub fn is_simple_constant(&self) -> bool {
matches!( matches!(
self, self,
Self::String { .. } | Self::Int { .. } | Self::ByteArray { .. } Self::String { .. } | Self::UInt { .. } | Self::ByteArray { .. }
) )
} }
} }

View File

@ -702,6 +702,10 @@ impl<'comments> Formatter<'comments> {
} }
pub fn int<'a>(&mut self, s: &'a str, base: &Base) -> Document<'a> { pub fn int<'a>(&mut self, s: &'a str, base: &Base) -> Document<'a> {
unimplemented!()
}
pub fn uint<'a>(&mut self, s: &'a str, base: &Base) -> Document<'a> {
match base { match base {
Base::Decimal { numeric_underscore } if *numeric_underscore => { Base::Decimal { numeric_underscore } if *numeric_underscore => {
let s = s let s = s
@ -755,7 +759,7 @@ impl<'comments> Formatter<'comments> {
one_liner, one_liner,
} => self.pipeline(expressions, *one_liner), } => self.pipeline(expressions, *one_liner),
UntypedExpr::Int { value, base, .. } => self.int(value, base), UntypedExpr::UInt { value, base, .. } => self.uint(value, base),
UntypedExpr::String { value, .. } => self.string(value), UntypedExpr::String { value, .. } => self.string(value),

View File

@ -221,7 +221,7 @@ impl<'a> CodeGenerator<'a> {
pub(crate) fn build(&mut self, body: &TypedExpr, ir_stack: &mut AirStack) { pub(crate) fn build(&mut self, body: &TypedExpr, ir_stack: &mut AirStack) {
match body { match body {
TypedExpr::Int { value, .. } => ir_stack.integer(value.to_string()), TypedExpr::UInt { value, .. } => ir_stack.integer(value.to_string()),
TypedExpr::String { value, .. } => ir_stack.string(value.to_string()), TypedExpr::String { value, .. } => ir_stack.string(value.to_string()),
TypedExpr::ByteArray { bytes, .. } => ir_stack.byte_array(bytes.to_vec()), TypedExpr::ByteArray { bytes, .. } => ir_stack.byte_array(bytes.to_vec()),
TypedExpr::Pipeline { expressions, .. } | TypedExpr::Sequence { expressions, .. } => { TypedExpr::Pipeline { expressions, .. } | TypedExpr::Sequence { expressions, .. } => {

View File

@ -2,9 +2,7 @@ use chumsky::prelude::*;
use crate::{ use crate::{
ast, ast,
parser::{ parser::{annotation, error::ParseError, literal::bytearray, token::Token, utils},
annotation, error::ParseError, literal::bytearray::parser as bytearray, token::Token, utils,
},
}; };
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> { pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {

View File

@ -1,8 +1,6 @@
use chumsky::prelude::*; use chumsky::prelude::*;
use crate::parser::{ use crate::parser::{error::ParseError, expr::UntypedExpr, literal::bytearray, token::Token};
error::ParseError, expr::UntypedExpr, literal::bytearray::parser as bytearray, token::Token,
};
pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> { pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
bytearray(|bytes, preferred_format, location| UntypedExpr::ByteArray { bytearray(|bytes, preferred_format, location| UntypedExpr::ByteArray {

View File

@ -2,11 +2,11 @@ use chumsky::prelude::*;
use crate::{ use crate::{
expr::UntypedExpr, expr::UntypedExpr,
parser::{error::ParseError, literal::int::parser as int, token::Token}, parser::{error::ParseError, literal::uint, token::Token},
}; };
pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> { pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
int().map_with_span(|(value, base), span| UntypedExpr::Int { uint().map_with_span(|(value, base), span| UntypedExpr::UInt {
location: span, location: span,
value, value,
base, base,

View File

@ -32,7 +32,7 @@ Fn {
location: 21..22, location: 21..22,
name: "a", name: "a",
}, },
right: Int { right: UInt {
location: 25..26, location: 25..26,
value: "1", value: "1",
base: Decimal { base: Decimal {

View File

@ -9,7 +9,7 @@ Assignment {
expressions: [ expressions: [
Assignment { Assignment {
location: 12..21, location: 12..21,
value: Int { value: UInt {
location: 20..21, location: 20..21,
value: "4", value: "4",
base: Decimal { base: Decimal {
@ -30,7 +30,7 @@ Assignment {
location: 25..26, location: 25..26,
name: "x", name: "x",
}, },
right: Int { right: UInt {
location: 29..30, location: 29..30,
value: "5", value: "5",
base: Decimal { base: Decimal {

View File

@ -12,7 +12,7 @@ Sequence {
CallArg { CallArg {
label: None, label: None,
location: 16..17, location: 16..17,
value: Int { value: UInt {
location: 16..17, location: 16..17,
value: "3", value: "3",
base: Decimal { base: Decimal {
@ -124,21 +124,21 @@ Sequence {
value: List { value: List {
location: 77..88, location: 77..88,
elements: [ elements: [
Int { UInt {
location: 79..80, location: 79..80,
value: "1", value: "1",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 82..83, location: 82..83,
value: "2", value: "2",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 85..86, location: 85..86,
value: "3", value: "3",
base: Decimal { base: Decimal {

View File

@ -13,14 +13,14 @@ If {
body: BinOp { body: BinOp {
location: 12..17, location: 12..17,
name: AddInt, name: AddInt,
left: Int { left: UInt {
location: 12..13, location: 12..13,
value: "1", value: "1",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
right: Int { right: UInt {
location: 16..17, location: 16..17,
value: "1", value: "1",
base: Decimal { base: Decimal {
@ -38,7 +38,7 @@ If {
location: 28..29, location: 28..29,
name: "a", name: "a",
}, },
right: Int { right: UInt {
location: 32..33, location: 32..33,
value: "1", value: "1",
base: Decimal { base: Decimal {
@ -46,7 +46,7 @@ If {
}, },
}, },
}, },
body: Int { body: UInt {
location: 38..39, location: 38..39,
value: "3", value: "3",
base: Decimal { base: Decimal {
@ -56,7 +56,7 @@ If {
location: 28..41, location: 28..41,
}, },
], ],
final_else: Int { final_else: UInt {
location: 51..52, location: 51..52,
value: "4", value: "4",
base: Decimal { base: Decimal {

View File

@ -2,7 +2,7 @@
source: crates/aiken-lang/src/parser/expr/int.rs source: crates/aiken-lang/src/parser/expr/int.rs
description: "Code:\n\n0x01" description: "Code:\n\n0x01"
--- ---
Int { UInt {
location: 0..4, location: 0..4,
value: "1", value: "1",
base: Hexadecimal, base: Hexadecimal,

View File

@ -5,21 +5,21 @@ description: "Code:\n\n[1, 2, 3]"
List { List {
location: 0..9, location: 0..9,
elements: [ elements: [
Int { UInt {
location: 1..2, location: 1..2,
value: "1", value: "1",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 4..5, location: 4..5,
value: "2", value: "2",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 7..8, location: 7..8,
value: "3", value: "3",
base: Decimal { base: Decimal {

View File

@ -2,7 +2,7 @@
source: crates/aiken-lang/src/parser/expr/int.rs source: crates/aiken-lang/src/parser/expr/int.rs
description: "Code:\n\n1" description: "Code:\n\n1"
--- ---
Int { UInt {
location: 0..1, location: 0..1,
value: "1", value: "1",
base: Decimal { base: Decimal {

View File

@ -5,7 +5,7 @@ description: "Code:\n\n-1"
UnOp { UnOp {
op: Negate, op: Negate,
location: 0..2, location: 0..2,
value: Int { value: UInt {
location: 1..2, location: 1..2,
value: "1", value: "1",
base: Decimal { base: Decimal {

View File

@ -7,7 +7,7 @@ Sequence {
expressions: [ expressions: [
Assignment { Assignment {
location: 4..21, location: 4..21,
value: Int { value: UInt {
location: 12..21, location: 12..21,
value: "1234567", value: "1234567",
base: Decimal { base: Decimal {
@ -23,7 +23,7 @@ Sequence {
}, },
Assignment { Assignment {
location: 24..41, location: 24..41,
value: Int { value: UInt {
location: 32..41, location: 32..41,
value: "1000000", value: "1000000",
base: Decimal { base: Decimal {
@ -42,7 +42,7 @@ Sequence {
value: UnOp { value: UnOp {
op: Negate, op: Negate,
location: 52..59, location: 52..59,
value: Int { value: UInt {
location: 53..59, location: 53..59,
value: "10000", value: "10000",
base: Decimal { base: Decimal {

View File

@ -7,14 +7,14 @@ Assignment {
value: List { value: List {
location: 12..23, location: 12..23,
elements: [ elements: [
Int { UInt {
location: 14..15, location: 14..15,
value: "1", value: "1",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 17..18, location: 17..18,
value: "2", value: "2",
base: Decimal { base: Decimal {

View File

@ -5,14 +5,14 @@ description: "Code:\n\n[1, 2, ..[]]"
List { List {
location: 0..12, location: 0..12,
elements: [ elements: [
Int { UInt {
location: 1..2, location: 1..2,
value: "1", value: "1",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 4..5, location: 4..5,
value: "2", value: "2",
base: Decimal { base: Decimal {

View File

@ -10,28 +10,28 @@ Sequence {
value: Tuple { value: Tuple {
location: 12..24, location: 12..24,
elems: [ elems: [
Int { UInt {
location: 13..14, location: 13..14,
value: "1", value: "1",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 16..17, location: 16..17,
value: "2", value: "2",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 19..20, location: 19..20,
value: "3", value: "3",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
Int { UInt {
location: 22..23, location: 22..23,
value: "4", value: "4",
base: Decimal { base: Decimal {

View File

@ -12,7 +12,7 @@ Sequence {
CallArg { CallArg {
label: None, label: None,
location: 12..14, location: 12..14,
value: Int { value: UInt {
location: 12..14, location: 12..14,
value: "14", value: "14",
base: Decimal { base: Decimal {
@ -41,7 +41,7 @@ Sequence {
location: 17..18, location: 17..18,
name: "a", name: "a",
}, },
Int { UInt {
location: 20..22, location: 20..22,
value: "42", value: "42",
base: Decimal { base: Decimal {

View File

@ -11,7 +11,7 @@ PipeLine {
location: 0..1, location: 0..1,
name: "a", name: "a",
}, },
right: Int { right: UInt {
location: 4..5, location: 4..5,
value: "2", value: "2",
base: Decimal { base: Decimal {

View File

@ -9,7 +9,7 @@ BinOp {
location: 0..1, location: 0..1,
name: "a", name: "a",
}, },
right: Int { right: UInt {
location: 4..5, location: 4..5,
value: "1", value: "1",
base: Decimal { base: Decimal {

View File

@ -36,7 +36,7 @@ Call {
"thing", "thing",
), ),
location: 27..35, location: 27..35,
value: Int { value: UInt {
location: 34..35, location: 34..35,
value: "2", value: "2",
base: Decimal { base: Decimal {

View File

@ -36,7 +36,7 @@ Call {
"thing", "thing",
), ),
location: 39..47, location: 39..47,
value: Int { value: UInt {
location: 46..47, location: 46..47,
value: "2", value: "2",
base: Decimal { base: Decimal {

View File

@ -7,7 +7,7 @@ Call {
CallArg { CallArg {
label: None, label: None,
location: 18..19, location: 18..19,
value: Int { value: UInt {
location: 18..19, location: 18..19,
value: "1", value: "1",
base: Decimal { base: Decimal {

View File

@ -2,10 +2,7 @@ use chumsky::prelude::*;
use crate::{ use crate::{
expr::UntypedExpr, expr::UntypedExpr,
parser::{ parser::{error::ParseError, literal::string, literal::utf8_string, token::Token},
error::ParseError, literal::bytearray::utf8_string, literal::string::parser as string,
token::Token,
},
}; };
pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> { pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {

View File

@ -39,7 +39,7 @@ When {
), ),
}, },
), ),
then: Int { then: UInt {
location: 28..29, location: 28..29,
value: "3", value: "3",
base: Decimal { base: Decimal {
@ -78,7 +78,7 @@ When {
expressions: [ expressions: [
Assignment { Assignment {
location: 51..66, location: 51..66,
value: Int { value: UInt {
location: 65..66, location: 65..66,
value: "5", value: "5",
base: Decimal { base: Decimal {
@ -111,7 +111,7 @@ When {
}, },
], ],
guard: None, guard: None,
then: Int { then: UInt {
location: 90..91, location: 90..91,
value: "9", value: "9",
base: Decimal { base: Decimal {
@ -128,7 +128,7 @@ When {
}, },
], ],
guard: None, guard: None,
then: Int { then: UInt {
location: 99..100, location: 99..100,
value: "4", value: "4",
base: Decimal { base: Decimal {

View File

@ -1,3 +1,7 @@
pub(crate) mod bytearray; mod bytearray;
pub(crate) mod int; mod string;
pub(crate) mod string; mod uint;
pub use bytearray::{array_of_bytes, hex_string, parser as bytearray, utf8_string};
pub use string::parser as string;
pub use uint::parser as uint;

View File

@ -26,7 +26,7 @@ Module {
kind: Let, kind: Let,
annotation: None, annotation: None,
}, },
Int { UInt {
location: 30..32, location: 30..32,
value: "40", value: "40",
base: Decimal { base: Decimal {
@ -64,7 +64,7 @@ Module {
kind: Let, kind: Let,
annotation: None, annotation: None,
}, },
Int { UInt {
location: 67..69, location: 67..69,
value: "40", value: "40",
base: Decimal { base: Decimal {
@ -91,14 +91,14 @@ Module {
value: BinOp { value: BinOp {
location: 98..102, location: 98..102,
name: AddInt, name: AddInt,
left: Int { left: UInt {
location: 98..100, location: 98..100,
value: "40", value: "40",
base: Decimal { base: Decimal {
numeric_underscore: false, numeric_underscore: false,
}, },
}, },
right: Int { right: UInt {
location: 101..102, location: 101..102,
value: "2", value: "2",
base: Decimal { base: Decimal {
@ -136,7 +136,7 @@ Module {
CallArg { CallArg {
label: None, label: None,
location: 134..136, location: 134..136,
value: Int { value: UInt {
location: 134..136, location: 134..136,
value: "42", value: "42",
base: Decimal { base: Decimal {
@ -168,7 +168,7 @@ Module {
location: 141..142, location: 141..142,
name: "a", name: "a",
}, },
right: Int { right: UInt {
location: 145..147, location: 145..147,
value: "14", value: "14",
base: Decimal { base: Decimal {
@ -176,7 +176,7 @@ Module {
}, },
}, },
}, },
right: Int { right: UInt {
location: 151..153, location: 151..153,
value: "42", value: "42",
base: Decimal { base: Decimal {

View File

@ -199,9 +199,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
UntypedExpr::Var { location, name, .. } => self.infer_var(name, location), UntypedExpr::Var { location, name, .. } => self.infer_var(name, location),
UntypedExpr::Int { UntypedExpr::UInt {
location, value, .. location, value, ..
} => Ok(self.infer_int(value, location)), } => Ok(self.infer_uint(value, location)),
UntypedExpr::Sequence { UntypedExpr::Sequence {
expressions, expressions,
@ -1520,8 +1520,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
Ok((args, body)) Ok((args, body))
} }
fn infer_int(&mut self, value: String, location: Span) -> TypedExpr { fn infer_uint(&mut self, value: String, location: Span) -> TypedExpr {
TypedExpr::Int { TypedExpr::UInt {
location, location,
value, value,
tipo: int(), tipo: int(),
@ -1943,7 +1943,7 @@ fn assert_no_assignment(expr: &UntypedExpr) -> Result<(), Error> {
| UntypedExpr::ErrorTerm { .. } | UntypedExpr::ErrorTerm { .. }
| UntypedExpr::FieldAccess { .. } | UntypedExpr::FieldAccess { .. }
| UntypedExpr::If { .. } | UntypedExpr::If { .. }
| UntypedExpr::Int { .. } | UntypedExpr::UInt { .. }
| UntypedExpr::List { .. } | UntypedExpr::List { .. }
| UntypedExpr::PipeLine { .. } | UntypedExpr::PipeLine { .. }
| UntypedExpr::RecordUpdate { .. } | UntypedExpr::RecordUpdate { .. }