Skip to content

Commit

Permalink
Allow conversion to void* type
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Feb 2, 2024
1 parent 9f9d98c commit 4cfed68
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
8 changes: 7 additions & 1 deletion CLanguage/Compiler/EmitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ public void EmitCast (CType fromType, CType toType)
Emit (op);
// This conversion is implicit with how the evaluator stores its stuff
}
else if (fromBasicType != null && fromBasicType.IsIntegral && toType is CPointerType && fromBasicType.NumValues == toType.NumValues) {
else if (fromBasicType != null && fromBasicType.IsIntegral && toType is CPointerType) {
// Support `const char *p = 0;`
}
else if (fromType is CArrayType fat && toType is CPointerType tpt && fat.ElementType.NumValues == tpt.InnerType.NumValues) {
// Demote arrays to pointers
}
else if (fromType is CArrayType && toType.IsVoidPointer) {
// Demote arrays to void pointers without size check. Better hope sizeof works.
}
else if (fromType.IsPointer && toType.IsVoidPointer) {
// Demote pointers to void pointers without size check. Better hope sizeof works.
}
else if (fromType is CEnumType et && toType is CIntType bt) {
// Enums act like ints
}
Expand Down
32 changes: 21 additions & 11 deletions CLanguage/Types/CType.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
using System;
using System;
using System.Linq;
using System.Text;
using CLanguage.Interpreter;
using CLanguage.Interpreter;
using CLanguage.Syntax;
using CLanguage.Compiler;
using CLanguage.Compiler;

namespace CLanguage.Types
{
public abstract class CType
{
public TypeQualifiers TypeQualifiers { get; set; }

public abstract int GetByteSize (EmitContext c);
public abstract int GetByteSize (EmitContext c);
public abstract int NumValues { get; }

public static readonly CVoidType Void = new CVoidType ();

public virtual bool IsIntegral => false;

public virtual bool IsVoid => false;

readonly Lazy<CPointerType> pointer;

public CPointerType Pointer => pointer.Value;

public virtual bool IsVoid => false;

readonly Lazy<CPointerType> pointer;

public CPointerType Pointer => pointer.Value;

public bool IsVoidPointer => this switch {
CPointerType pt => pt.InnerType.IsVoid || pt.InnerType.IsVoidPointer,
_ => false,
};

public bool IsPointer => this switch {
CPointerType pt => true,
_ => false,
};

public CType ()
{
{
pointer = new Lazy<CPointerType> (CreatePointerType);
}

Expand Down
25 changes: 20 additions & 5 deletions CLanguageTests/OverloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace CLanguage.Tests
[TestClass]
public class OverloadTests
{
CInterpreter Run (string code)
static CInterpreter Run (string code)
{
var fullCode = "void start() { __cinit(); main(); } " + code;
var i = CLanguageService.CreateInterpreter (fullCode, new ArduinoTestMachineInfo (), printer: new TestPrinter ());
i.Reset ("start");
i.Step ();
i.Run ();
return i;
}

Expand Down Expand Up @@ -102,13 +102,28 @@ void main () {
}

[TestMethod]
public void PrintlnConstCharPtr ()
public void VoidPtrFromArray ()
{
var i = Run (@"
int f(void *x) { return 1; }
int xs[5] = { 10, 20, 30, 40, 50 };
void main () {
Serial.println(""hello"");
int fr = f(xs);
assertAreEqual (1, fr);
}");
}

[TestMethod]
public void VoidPtrFromOtherPtr ()
{
var i = Run (@"
int f(void *x) { return 1; }
int xs[5] = { 10, 20, 30, 40, 50 };
void main () {
int *p = xs;
int fr = f(p);
assertAreEqual (1, fr);
}");
Assert.AreEqual("hello\n", ((ArduinoTestMachineInfo)i.Executable.MachineInfo).Arduino.SerialOut.ToString());
}
}
}

0 comments on commit 4cfed68

Please sign in to comment.