Skip to content

Commit

Permalink
Unify string types under a single StrType.
Browse files Browse the repository at this point in the history
  • Loading branch information
alpacaaa committed Sep 11, 2023
1 parent 436fae7 commit cd648c7
Show file tree
Hide file tree
Showing 50 changed files with 498 additions and 98 deletions.
9 changes: 7 additions & 2 deletions compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,17 @@ pub enum Literal {
Int(i64),
Float(f64),
Bool(bool),
String(String),
MultiString(Vec<String>),
String(StrType),
Char(String), // Rust char and Go rune are slightly different, just fallback to a String
Slice(Vec<Expr>),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum StrType {
Single(String), // "quoted string"
Multi(Vec<String>), // multi-line string
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Operator {
Add,
Expand Down
14 changes: 8 additions & 6 deletions compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ast::{
Arm, Binding, Constructor, DebugKind, EnumDefinition, EnumFieldDef, Expr, File, FileId,
Function, FunctionKind, Generic, InterfaceSuperTrait, Literal, Loop, LoopFlow, Operator,
Pat, SelectArm, SelectArmPat, Span, StructDefinition, StructField, UnOp,
Pat, SelectArm, SelectArmPat, Span, StrType, StructDefinition, StructField, UnOp,
},
global_state::Module,
infer,
Expand Down Expand Up @@ -832,11 +832,13 @@ if {new_is_matching} != 1 {{
Literal::Int(n) => n.to_string(),
Literal::Float(n) => n.to_string(),
Literal::Bool(b) => b.to_string(),
Literal::String(s) => format!("\"{}\"", s).replace('\n', "\\n"),
Literal::MultiString(lines) => {
let body = lines.join("\n");
format!("`{}`", body)
}
Literal::String(inner) => match inner {
StrType::Single(s) => format!("\"{}\"", s).replace('\n', "\\n"),
StrType::Multi(lines) => {
let body = lines.join("\n");
format!("`{}`", body)
}
},
Literal::Char(s) => {
format!("\'{}\'", s)
}
Expand Down
10 changes: 0 additions & 10 deletions compiler/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,6 @@ impl Infer {
}
}

Literal::MultiString(s) => {
self.add_constraint(expected, &self.type_string(), &span);

Expr::Literal {
lit: Literal::MultiString(s),
ty: self.type_string(),
span,
}
}

Literal::Char(s) => {
self.add_constraint(expected, &self.type_char(), &span);

Expand Down
18 changes: 8 additions & 10 deletions compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
ast::{
Arm, Binding, Constructor, EnumDefinition, EnumFieldDef, Expr, FileId, Function,
FunctionKind, Generic, InterfaceSuperTrait, LineColumn, Literal, Loop, LoopFlow,
NewtypeDefinition, Operator, Pat, PkgImport, SelectArm, SelectArmPat, Span,
NewtypeDefinition, Operator, Pat, PkgImport, SelectArm, SelectArmPat, Span, StrType,
StructDefinition, StructField, StructFieldDef, StructFieldPat, TypeAliasDef, TypeAst, UnOp,
},
lexer::{Lexer, Pos, Token, TokenKind, TokenKind::EOF},
Expand Down Expand Up @@ -300,7 +300,7 @@ impl Parser {

TokenKind::Bool => Literal::Bool(self.tok.text == "true"),

TokenKind::String => Literal::String(self.tok.text.clone()),
TokenKind::String => Literal::String(StrType::Single(self.tok.text.clone())),

TokenKind::Char => Literal::Char(self.tok.text.clone()),

Expand Down Expand Up @@ -642,7 +642,7 @@ impl Parser {
*/
}

let lit = Literal::MultiString(lines);
let lit = Literal::String(StrType::Multi(lines));

Expr::Literal {
lit,
Expand Down Expand Up @@ -965,14 +965,12 @@ impl Parser {
let args = self.parse_paren(ParseParen::Paren);
let text = match args.first() {
Some(Expr::Literal {
lit: Literal::String(s),
lit: Literal::String(inner),
..
}) => s.to_string(),

Some(Expr::Literal {
lit: Literal::MultiString(lines),
..
}) => lines.join("\n"),
}) => match inner {
StrType::Single(s) => s.to_string(),
StrType::Multi(lines) => lines.join("\n"),
},

_ => {
self.error("invalid call to rawgo!".to_string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ No errors.
"expr": {
"Literal": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down Expand Up @@ -186,7 +188,9 @@ No errors.
{
"Literal": {
"lit": {
"String": "bar"
"String": {
"Single": "bar"
}
},
"ty": {
"Con": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ No errors.
"expr": {
"Literal": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ No errors.
"value": {
"Literal": {
"lit": {
"String": "mutable"
"String": {
"Single": "mutable"
}
},
"ty": {
"Con": {
Expand Down Expand Up @@ -125,7 +127,9 @@ No errors.
"value": {
"Literal": {
"lit": {
"String": "block"
"String": {
"Single": "block"
}
},
"ty": {
"Con": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ No errors.
{
"Literal": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,9 @@ No errors.
{
"Literal": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ No errors.
{
"Literal": {
"lit": {
"String": "hello"
"String": {
"Single": "hello"
}
},
"ty": {
"Con": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ No errors.
"value": {
"Literal": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down Expand Up @@ -171,7 +173,9 @@ No errors.
"pat": {
"Lit": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down
4 changes: 3 additions & 1 deletion compiler/test/snapshot/infer-expr/match-expressions.exp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ No errors.
"expr": {
"Literal": {
"lit": {
"String": "foo"
"String": {
"Single": "foo"
}
},
"ty": {
"Con": {
Expand Down
4 changes: 3 additions & 1 deletion compiler/test/snapshot/infer-expr/match-weirdness.exp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ No errors.
{
"Literal": {
"lit": {
"String": "asdf"
"String": {
"Single": "asdf"
}
},
"ty": {
"Con": {
Expand Down
Loading

0 comments on commit cd648c7

Please sign in to comment.