Skip to content

Commit

Permalink
Downto 204 resolution errors. Fixed handling of tuples. Types can now…
Browse files Browse the repository at this point in the history
… be used as expressions. Just the name is the same as using them as a constructor.
  • Loading branch information
cdiggins committed Mar 21, 2024
1 parent 083437d commit 02a59e7
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 47 deletions.
3 changes: 2 additions & 1 deletion Plato.AST/AstNodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ public static AstNode ToAst(this CstParenthesizedExpression expr)
if (expr.Expression.Nodes.Count == 1)
return new AstParenthesized(expr, ToAst(expr.Expression.Node));

return ToIntrinsicInvocation(expr, "Tuple", expr.Expression.Nodes.Select(ToAst).ToArray());
var arity = expr.Expression.Nodes.Count;
return ToIntrinsicInvocation(expr, $"Tuple{arity}", expr.Expression.Nodes.Select(ToAst).ToArray());
}

public static AstNode ToAst(this CstLeafExpression expr)
Expand Down
7 changes: 2 additions & 5 deletions Plato.CSharpWriter/SymbolWriterCSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ public SymbolWriterCSharp Write(Symbol symbol)
{
switch (symbol)
{
case TypeDefinition typeDefinition:
return Write(typeDefinition);
case DefinitionSymbol definition:
return Write(definition);
case Expression expression:
return Write(expression);
case Statement statement:
return Write(statement);
case TypeDefinition typeDefinition:
return Write(typeDefinition);
case TypeExpression typeExpression:
return Write(typeExpression);
default:
Expand Down Expand Up @@ -644,9 +644,6 @@ public SymbolWriterCSharp Write(DefinitionSymbol value)

case VariableDefinition variable:
return Write("var ").Write(variable.Name).Write(" = ").Write(variable.Value).WriteLine(";");

case PredefinedDefinition predefined:
return Write(predefined.Name);
}

return this;
Expand Down
23 changes: 6 additions & 17 deletions PlatoCompiler/Symbols/Definitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Plato.Compiler.Symbols
{
public abstract class DefinitionSymbol : Symbol
{
// NOTE: type definitions, have no type expression. It gets circular and confusing.
public TypeExpression Type { get; }

public override string Name { get; }
Expand All @@ -24,19 +25,6 @@ protected DefinitionSymbol(TypeExpression type, string name)
public abstract Reference ToReference();
}

public class PredefinedDefinition : DefinitionSymbol
{
public PredefinedDefinition(TypeExpression typeRef, string name)
: base(typeRef, name)
{ }

public override Reference ToReference()
=> new PredefinedReference(this);

public override IEnumerable<Symbol> GetChildSymbols()
=> Enumerable.Empty<Symbol>();
}

public class FunctionDefinition : DefinitionSymbol, IFunction
{
public IReadOnlyList<ParameterDefinition> Parameters { get; }
Expand Down Expand Up @@ -100,7 +88,7 @@ public override IEnumerable<Symbol> GetChildSymbols()
=> new[] { Type };
}

public class TypeDefinition : Symbol
public class TypeDefinition : DefinitionSymbol
{
public TypeKind Kind { get; }

Expand All @@ -116,13 +104,11 @@ public class TypeDefinition : Symbol
public List<TypeExpression> Implements { get; } = new List<TypeExpression>();
public List<FunctionDefinition> CompilerGeneratedFunctions { get; } = new List<FunctionDefinition>();

public override string Name { get; }

public SelfType Self { get; }

public TypeDefinition(TypeKind kind, string name)
: base(null, name)
{
Name = name;
Kind = kind;
if (!this.IsTypeVariable())
Self = new SelfType();
Expand Down Expand Up @@ -173,6 +159,9 @@ public TypeExpression ToTypeExpression()
public override string ToString()
=> $"{Name}_{Id}:{Kind}";

public override Reference ToReference()
=> new TypeReference(this);

public override IEnumerable<Symbol> GetChildSymbols()
=> Methods.Cast<Symbol>().Concat(Fields).Concat(TypeParameters).Concat(Inherits).Concat(Implements);

Expand Down
6 changes: 3 additions & 3 deletions PlatoCompiler/Symbols/Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ protected Reference(DefinitionSymbol def)
public override string ToString() => Name;
}

public class PredefinedReference : Reference
public class TypeReference : Reference
{
public PredefinedReference(PredefinedDefinition def)
public TypeReference(TypeDefinition def)
: base(def)
{ }

public new PredefinedDefinition Definition => base.Definition as PredefinedDefinition;
public new TypeDefinition Definition => base.Definition as TypeDefinition;
}

public class VariableReference : Reference
Expand Down
11 changes: 8 additions & 3 deletions PlatoCompiler/Symbols/SymbolFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public SymbolFactory(ILogger logger)

public List<TypeDefinition> TypeDefs { get; } = new List<TypeDefinition>();


public T BindValue<T>(string name, T value) where T : Symbol
{
ValueBindingsScope = ValueBindingsScope.Bind(name, value);
Expand Down Expand Up @@ -90,8 +89,14 @@ public Reference GetValue(string name, AstNode node)
var sym = ValueBindingsScope.GetValue(name);
if (sym == null)
{
LogError($"Could not find symbol {name}", node);
return null;
var td = GetTypeDefinition(name);
if (td == null)
{
LogError($"Could not find symbol {name}", node);
return null;
}

return td.ToReference();
}

if (sym is DefinitionSymbol def)
Expand Down
12 changes: 0 additions & 12 deletions PlatoCompiler/Types/FunctionAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,6 @@ public IType Process(Expression expr)
r = Process(parenthesized);
break;

case PredefinedReference predefinedReference:
r = ToIType(predefinedReference.Definition.Type);
break;

case VariableReference variableReference:
r = ToIType(variableReference.Definition.Type);
break;
Expand Down Expand Up @@ -486,14 +482,6 @@ public IType Process(FunctionCall fc)
return bestFunction.DeterminedReturnType;
}

if (fc.Function is PredefinedReference pr)
{
if (pr.Definition.Name == "Tuple")
return CreateTuple(argTypes);

throw new Exception($"Unrecognized predefined definition {pr.Definition.Name}");
}

if (fc.Function is ParameterReference paramRef)
{
// Look at the declared parameter type. If there is none, we are going to create an
Expand Down
6 changes: 0 additions & 6 deletions PlatoCompiler/Vsg/VisualSyntaxGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ public VsgNode CreateNode(DefinitionSymbol symbol)
case ParameterDefinition parameterSymbol:
return CreateNode($"{parameterSymbol.Name} as Function");

case PredefinedDefinition predefinedSymbol:
return CreateNode(predefinedSymbol);

case VariableDefinition variableSymbol:
break;
}
Expand Down Expand Up @@ -223,9 +220,6 @@ public VsgSocket GetSocket(Symbol expr)
return CreateNode(expr)?.MainOutput;
}

public VsgNode CreateNode(PredefinedDefinition pds)
=> CreateNode(pds.Name);

public VsgNode CreateNode(Literal lit)
=> CreateNode(lit.Value.ToLiteralString());

Expand Down

0 comments on commit 02a59e7

Please sign in to comment.