From 39656a3801bb311edd9ebb65e92a24fc48f69ec7 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 14 Mar 2021 11:53:13 -0400 Subject: Add string support to the frontend --- src/tc/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/tc/mod.rs') diff --git a/src/tc/mod.rs b/src/tc/mod.rs index b5acfac2b4..2c40a02bf7 100644 --- a/src/tc/mod.rs +++ b/src/tc/mod.rs @@ -49,6 +49,7 @@ pub enum PrimType { Int, Float, Bool, + CString, } impl From for ast::Type { @@ -57,6 +58,7 @@ impl From for ast::Type { PrimType::Int => ast::Type::Int, PrimType::Float => ast::Type::Float, PrimType::Bool => ast::Type::Bool, + PrimType::CString => ast::Type::CString, } } } @@ -67,6 +69,7 @@ impl Display for PrimType { PrimType::Int => f.write_str("int"), PrimType::Float => f.write_str("float"), PrimType::Bool => f.write_str("bool"), + PrimType::CString => f.write_str("cstring"), } } } @@ -125,6 +128,7 @@ impl TryFrom for ast::Type { const INT: Type = Type::Prim(PrimType::Int); const FLOAT: Type = Type::Prim(PrimType::Float); const BOOL: Type = Type::Prim(PrimType::Bool); +const CSTRING: Type = Type::Prim(PrimType::CString); impl Display for Type { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -144,6 +148,7 @@ impl From for Type { ast::Type::Int => INT, ast::Type::Float => FLOAT, ast::Type::Bool => BOOL, + ast::Type::CString => CSTRING, ast::Type::Function(ast::FunctionType { args, ret }) => Type::Fun { args: args.into_iter().map(Self::from).collect(), ret: Box::new(Self::from(*ret)), @@ -181,8 +186,9 @@ impl<'ast> Typechecker<'ast> { let type_ = match lit { Literal::Int(_) => Type::Prim(PrimType::Int), Literal::Bool(_) => Type::Prim(PrimType::Bool), + Literal::String(_) => Type::Prim(PrimType::CString), }; - Ok(hir::Expr::Literal(lit, type_)) + Ok(hir::Expr::Literal(lit.to_owned(), type_)) } ast::Expr::UnaryOp { op, rhs } => todo!(), ast::Expr::BinaryOp { lhs, op, rhs } => { -- cgit 1.4.1