Add string parsing and a print builtin (#721)

It was in my mind to add String in order to support libraries in `package`.  `print` is added in order to have a String go to stdout. I've tried to do `print` in a way that won't be too hard to add other printable types, but it's probably also somewhat optional here -- that is, if desired, I could remove it. But it was a lot easier to doublecheck `\n` behavior with it, and I suspect it'll be helpful in other tests if it supports more value types.

On the side, this also fixes dereferencing in Pattern/Expression Print() calls, which I was noticing printing pointers instead of values. This may be another argument for moving away from passing pointers, since this seems to be a difficult-to-catch error.

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Jon Meow
2021-08-11 13:14:05 -07:00
committed by GitHub
co-authored by Geoff Romer
parent ecb5a611e5
commit 250ce4ab00
36 changed files with 549 additions and 33 deletions
@@ -236,6 +236,14 @@ void Value::Print(llvm::raw_ostream& out) const {
// TODO: Find a way to print useful information about the continuation
// without creating a dependency cycle.
break;
case Value::Kind::StringType:
out << "String";
break;
case Value::Kind::StringValue:
out << "\"";
out.write_escaped(cast<StringValue>(*this).Val());
out << "\"";
break;
}
}
@@ -293,6 +301,10 @@ auto CopyVal(const Value* val, int line_num) -> const Value* {
return global_arena->New<AutoType>();
case Value::Kind::ContinuationType:
return global_arena->New<ContinuationType>();
case Value::Kind::StringType:
return global_arena->New<StringType>();
case Value::Kind::StringValue:
return global_arena->New<StringValue>(cast<StringValue>(*val).Val());
case Value::Kind::VariableType:
case Value::Kind::StructType:
case Value::Kind::ChoiceType:
@@ -339,6 +351,7 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
case Value::Kind::BoolType:
case Value::Kind::ContinuationType:
case Value::Kind::TypeType:
case Value::Kind::StringType:
return true;
case Value::Kind::VariableType:
return cast<VariableType>(*t1).Name() == cast<VariableType>(*t2).Name();
@@ -390,6 +403,8 @@ auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
case Value::Kind::TupleValue:
return FieldsValueEqual(cast<TupleValue>(*v1).Elements(),
cast<TupleValue>(*v2).Elements(), line_num);
case Value::Kind::StringValue:
return cast<StringValue>(*v1).Val() == cast<StringValue>(*v2).Val();
case Value::Kind::IntType:
case Value::Kind::BoolType:
case Value::Kind::TypeType:
@@ -400,6 +415,7 @@ auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
case Value::Kind::ChoiceType:
case Value::Kind::ContinuationType:
case Value::Kind::VariableType:
case Value::Kind::StringType:
return TypeEqual(v1, v2);
case Value::Kind::StructValue:
case Value::Kind::AlternativeValue: