Skip to content

Commit

Permalink
Fix bugs, support pointer add / subtract
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamQiufeng committed Aug 5, 2022
1 parent 1972b5e commit d1df006
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions PseudoCode.Core/Runtime/Instances/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public virtual T Get<T>()

public virtual string Represent()
{
if (Value == null) return "NULL";
return Type switch
{
DateType => Get<DateOnly>().ToString("dd/MM/yyyy"),
Expand Down
2 changes: 1 addition & 1 deletion PseudoCode.Core/Runtime/Operations/UnaryOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void MetaOperate()
Program.AnalyserFeedbacks.Add(new Feedback
{
Message =
$"This operation on {to} is either not supported or will be casted to another type at runtime",
$"{OperatorMethod} operation on {to} is either not supported or will be casted to another type at runtime",
Severity = Feedback.SeverityType.Warning,
SourceRange = SourceRange
});
Expand Down
34 changes: 33 additions & 1 deletion PseudoCode.Core/Runtime/Types/PointerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PseudoCode.Core.Runtime.Types;

public class PointerType : Type
public class PointerType : PrimitiveType<uint>
{
public override string Name => "POINTER";
public override uint Id => PointerId;
Expand All @@ -14,6 +14,24 @@ public PointerType(Scope parentScope, PseudoProgram program) : base(parentScope,
{
}

public override Type BinaryResultType(PseudoOperator type, Type right)
{
if (right is IntegerType)
{
return type switch
{
PseudoOperator.Add or PseudoOperator.Subtract => this,
_ => base.BinaryResultType(type, right)
};
}
return base.BinaryResultType(type, right);
}

public override Type UnaryResultType(PseudoOperator type)
{
return type is PseudoOperator.GetPointed ? PointedType : base.UnaryResultType(type);
}

public override bool IsConvertableFrom(Type type)
{
return type is PointerType;
Expand All @@ -29,4 +47,18 @@ public override Instance GetPointed(Instance i)

throw new InvalidAccessError($"Access to uninitialized address {address}", null);
}

public override Instance CastFrom(Instance i)
{
return Instance(Convert.ToUInt32(i.Value), ParentScope);
}
public override Instance Add(Instance i1, Instance i2)
{
return ArithmeticOperation(i1, i2, (arg1, arg2) => arg1 + arg2);
}

public override Instance Subtract(Instance i1, Instance i2)
{
return ArithmeticOperation(i1, i2, (arg1, arg2) => arg1 - arg2);
}
}
5 changes: 4 additions & 1 deletion PseudoCode.Core/run.pseudo
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ a <- 0
a <- -(a + a * -a POW 2 POW 3) DIV 100 MOD 100
c <- a < 63 - 2.3 OR (a > 65 OR a > 63) AND NOT a < 65 AND TRUE
Char <- '\n'
OUTPUT ^Char, iptr, ^Char^
DECLARE DatePtr : ^DATE
DatePtr <- (^Char) + 1
OUTPUT ^Char, iptr, DatePtr^ // DatePtr points to unassigned value
Date <- 28/02/2008 + 10/03/0010
OUTPUT DatePtr^
OUTPUT a + 2 + a, c, Char, Date
IF a <> 64 THEN
OUTPUT b
Expand Down

0 comments on commit d1df006

Please sign in to comment.