Skip to content

Commit

Permalink
[console] Print numbers directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Nov 15, 2019
1 parent a02b5a6 commit ca286aa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions CiTree.cs
Expand Up @@ -1082,6 +1082,11 @@ public override CiSymbol TryLookup(string name)
public override CiType PtrOrSelf { get { return new CiArrayPtrType { ElementType = this.ElementType, Modifier = CiToken.ExclamationMark }; } }
}

public class CiPrintableType : CiType
{
public override bool IsAssignableFrom(CiType right) { return right is CiStringType || right is CiNumericType; }
}

public class CiSystem : CiScope
{
public static readonly CiType NullType = new CiType();
Expand Down Expand Up @@ -1110,8 +1115,9 @@ public class CiSystem : CiScope
public static readonly CiMember ListCount = new CiMember { Name = "Count", Type = UIntType };
public static readonly CiMethod ListClear = new CiMethod(CiCallType.Normal, null, "Clear");
public static readonly CiMethod ListRemoveAt = new CiMethod(CiCallType.Normal, null, "RemoveAt", new CiVar(IntType, "index"));
public static readonly CiMethod ConsoleWrite = new CiMethod(CiCallType.Static, null, "Write", new CiVar(StringPtrType, "value"));
public static readonly CiMethod ConsoleWriteLine = new CiMethod(CiCallType.Static, null, "WriteLine", new CiVar(StringPtrType, "value") { Value = new CiLiteral("") });
public static readonly CiType PrintableType = new CiPrintableType();
public static readonly CiMethod ConsoleWrite = new CiMethod(CiCallType.Static, null, "Write", new CiVar(PrintableType, "value"));
public static readonly CiMethod ConsoleWriteLine = new CiMethod(CiCallType.Static, null, "WriteLine", new CiVar(PrintableType, "value") { Value = new CiLiteral("") });
public static readonly CiClass ConsoleBase = new CiClass(CiCallType.Static, "ConsoleBase",
ConsoleWrite,
ConsoleWriteLine);
Expand Down
9 changes: 9 additions & 0 deletions GenC.cs
Expand Up @@ -519,6 +519,15 @@ void WriteConsoleWrite(CiExpr obj, CiExpr[] args, bool newLine)
Write(error ? "fprintf(stderr, " : "printf(");
WritePrintf(interpolated, newLine);
}
else if (args[0].Type is CiNumericType) {
Write(error ? "fprintf(stderr, " : "printf(");
Write(args[0].Type is CiIntegerType ? "\"%d" : "\"%g");
if (newLine)
Write("\\n");
Write("\", ");
args[0].Accept(this, CiPriority.Statement);
Write(')');
}
else if (newLine && !error) {
Write("puts(");
args[0].Accept(this, CiPriority.Statement);
Expand Down

0 comments on commit ca286aa

Please sign in to comment.