Skip to content

Commit

Permalink
Numerous bugfixes, added console interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltasarq committed Nov 11, 2017
1 parent 7d67a41 commit ef40413
Show file tree
Hide file tree
Showing 159 changed files with 1,640 additions and 1,046 deletions.
18 changes: 6 additions & 12 deletions CSim.csproj
Expand Up @@ -40,7 +40,6 @@
<Folder Include="Res\" />
<Folder Include="Core\Types\" />
<Folder Include="Core\Opcodes\" />
<Folder Include="Core\Exceptions\" />
<Folder Include="Core\Types\Primitives\" />
<Folder Include="Core\Variables\" />
<Folder Include="Core\Functions\" />
Expand All @@ -58,30 +57,21 @@
<Compile Include="Core\AType.cs" />
<Compile Include="Core\Opcode.cs" />
<Compile Include="Core\Lexer.cs" />
<Compile Include="Core\Exceptions\UnknownTypeException.cs" />
<Compile Include="Core\Parser.cs" />
<Compile Include="Core\Exceptions\UnknownVbleException.cs" />
<Compile Include="Core\Exceptions\TypeMismatchException.cs" />
<Compile Include="Core\EngineException.cs" />
<Compile Include="Core\Exceptions\AlreadyExistingVbleException.cs" />
<Compile Include="Core\Exceptions\ExhaustedMemoryException.cs" />
<Compile Include="Core\exceptions.cs" />
<Compile Include="Core\Types\Ptr.cs" />
<Compile Include="Core\Reserved.cs" />
<Compile Include="Core\Types\Ref.cs" />
<Compile Include="Core\Exceptions\InvalidIdException.cs" />
<Compile Include="Core\Types\Primitives\Char.cs" />
<Compile Include="Core\Types\Primitives\Int.cs" />
<Compile Include="Core\Types\Primitive.cs" />
<Compile Include="Core\Variables\RefVariable.cs" />
<Compile Include="Core\Locale.cs" />
<Compile Include="Core\L18n.cs" />
<Compile Include="Core\Exceptions\InvalidMaxMemoryException.cs" />
<Compile Include="Core\Exceptions\IncorrectAddressException.cs" />
<Compile Include="Core\Function.cs" />
<Compile Include="Core\Functions\EmbeddedFunction.cs" />
<Compile Include="Core\Functions\UserFunction.cs" />
<Compile Include="Core\StdLib.cs" />
<Compile Include="Core\Exceptions\ParsingException.cs" />
<Compile Include="Core\RValue.cs" />
<Compile Include="Core\Literal.cs" />
<Compile Include="Core\Literals\IntLiteral.cs" />
Expand Down Expand Up @@ -145,7 +135,6 @@
<Compile Include="Ui\Drawer\GrphBoxedArray.cs" />
<Compile Include="Core\Variables\ArrayVariable.cs" />
<Compile Include="Ui\Drawer\GraphInfo.cs" />
<Compile Include="Core\ExpressionParser.cs" />
<Compile Include="Core\FunctionLibrary\TypedMalloc.cs" />
<Compile Include="Core\Variables\TypeVariable.cs" />
<Compile Include="Core\Types\TypeType.cs" />
Expand Down Expand Up @@ -192,6 +181,9 @@
<Compile Include="Core\Variables\IndirectVariable.cs" />
<Compile Include="Core\FunctionLibrary\StrCmp.cs" />
<Compile Include="Core\FunctionLibrary\StrCat.cs" />
<Compile Include="Ui\InputDialog.cs" />
<Compile Include="Core\FunctionLibrary\SPrintf.cs" />
<Compile Include="Core\FunctionLibrary\Fgets.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -218,6 +210,8 @@
<EmbeddedResource Include="Res\back.png" />
<EmbeddedResource Include="Res\play.png" />
<EmbeddedResource Include="Res\stop.png" />
<EmbeddedResource Include="Res\no.png" />
<EmbeddedResource Include="Res\yes.png" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>
Expand Down
43 changes: 42 additions & 1 deletion CSimTests/ArrayTests.cs
@@ -1,5 +1,8 @@
namespace CSimTests {
// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using System.Numerics;
using System.Linq;
using System.Collections.Generic;
using NUnit.Framework;

Expand Down Expand Up @@ -102,6 +105,44 @@ public void NotTypedArrayCreation()
Assert.AreEqual( (BigInteger) ( NumElements * this.int_t.Size ), array.Count );
}

[Test]
public void TestPtrArray()
{
const int NumElements = 2;
PtrVariable v = null;
Variable vble_x = null;
ArrayVariable ptr_v;

Assert.DoesNotThrow( () => {
vble_x = this.vm.Execute( "int xx = 5" );
this.vm.Execute( @"int ** ptr_v = new int*[" + NumElements + "]" );
v = (PtrVariable) this.vm.TDS.LookUp( "ptr_v" );
this.vm.Execute( @"ptr_v[ 0 ] = &xx" );
this.vm.Execute( @"ptr_v[ 1 ] = &xx" );
});

List<Variable> vbles = this.vm.TDS.LookForAllVblesInAddress(
v.LiteralValue.ToBigInteger() ).ToList();
ptr_v = (ArrayVariable) vbles[ 0 ];
int addrX = (int) vble_x.Address;
int ptrX0 = (int) ptr_v.ExtractArrayElementsValues()[ 0 ].Value.ToBigInteger();
int ptrX1 = (int) ptr_v.ExtractArrayElementsValues()[ 1 ].Value.ToBigInteger();

Assert.AreEqual( addrX, ptrX0 );
Assert.AreEqual( addrX, ptrX1 );

Assert.DoesNotThrow( () => {
this.vm.Execute( "*ptr_v[ 0 ] = *ptr_v[ 0 ] + 1" );
});

Assert.DoesNotThrow( () => {
this.vm.Execute( "*ptr_v[ 1 ] = *ptr_v[ 1 ] + 1" );
});

Assert.AreEqual( 7, (int) vble_x.LiteralValue.ToBigInteger() );
}

private Machine vm;
private Variable vble_v;
private AType int_t;
Expand Down
27 changes: 0 additions & 27 deletions CSimTests/AssemblyInfo.cs

This file was deleted.

1 change: 0 additions & 1 deletion CSimTests/CSimTests.csproj
Expand Up @@ -38,7 +38,6 @@
<Reference Include="System.Numerics" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="TypeTests.cs" />
<Compile Include="VbleTests.cs" />
<Compile Include="ParserTests.cs" />
Expand Down
76 changes: 74 additions & 2 deletions CSimTests/FunctionTests.cs
@@ -1,6 +1,10 @@
namespace CSimTests {
// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using System;
using System.Numerics;
using System.Text;
using System.Globalization;
using NUnit.Framework;

using CSim.Core;
Expand All @@ -14,7 +18,8 @@ public void Init()
Console.WriteLine( "Tests pass individually, but not globally, "
+ "due to static contents of functions." );

this.machine = new Machine();
this.machine = new Machine( outputter: (s) => {},
inputter: (s) => "Baltasar" );

Assert.DoesNotThrow( () => {
this.machine.Execute( @"int int_v;" );
Expand All @@ -40,6 +45,73 @@ public void TestFree()
});
}

[Test]
public void TestFGets()
{
const int Max = 20;
string cmd = "fgets(str, " + Max + ", stdin);";
string expectedResult = "Baltasar";
Variable str = null;
Variable vble= null;

Assert.DoesNotThrow( () => {
str = this.machine.Execute( "char *str = malloc(" + Max + ")" );
this.machine.Memory.CheckAddressFits( str.LiteralValue.ToBigInteger() );
});

Assert.Throws<TypeMismatchException>( () => {
this.machine.Execute( "fgets(1, 2, 3);" );
});

Assert.Throws<TypeMismatchException>( () => {
this.machine.Execute( "fgets(str, \"a\", \"b\" );" );
});

Assert.Throws<TypeMismatchException>( () => {
this.machine.Execute( "fgets(str, 2, \"b\" );" );
});

Assert.DoesNotThrow( () => {
vble = this.machine.Execute( cmd );
});

int address = (int) vble.LiteralValue.ToBigInteger();
byte[] result = this.machine.Memory.ReadStringFromMemory( address );
string dest = Encoding.ASCII.GetString( result );
Assert.AreEqual( (int) str.LiteralValue.ToBigInteger(), address );
Assert.AreEqual( expectedResult, dest );
}

[Test]
public void TestSPrintf()
{
const int Max = 20;
string msgFormat = "%s %d %f %c";
string msg = "hola";
int val1 = 42;
string val2 = 42.51.ToString( CultureInfo.InvariantCulture );
char val3 = (char) 42;
Variable str = null;
Variable vble = null;
string cmd = "sprintf(s, \""
+ msgFormat + "\", \"" + msg + "\", "
+ val1 + ", " + val2 + ", '" + val3 + "');";
string expectedResult = "hola 42 42.51 *";

Assert.DoesNotThrow( () => {
str = this.machine.Execute( "char *s = malloc(" + Max + ")" );
vble = this.machine.Execute( cmd );
this.machine.Memory.CheckAddressFits( str.LiteralValue.ToBigInteger() );
});

byte[] result = this.machine.Memory.ReadStringFromMemory(
str.Value.ToBigInteger() );

string dest = Encoding.ASCII.GetString( result );
Assert.AreEqual( expectedResult.Length, (int) vble.LiteralValue.ToBigInteger() );
Assert.AreEqual( expectedResult, dest );
}

[Test]
public void TestStrCmp()
{
Expand Down
6 changes: 3 additions & 3 deletions CSimTests/NumericTests.cs
@@ -1,9 +1,9 @@
namespace CSimTests {
using System.Numerics;
// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using NUnit.Framework;

using CSim.Core;
using CSim.Core.Variables;

[TestFixture]
public class NumericTests {
Expand Down
104 changes: 104 additions & 0 deletions CSimTests/ParserTests.cs
@@ -1,3 +1,4 @@
// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using System.Numerics;
Expand Down Expand Up @@ -70,6 +71,109 @@ public void TestPtrExpression()
Assert.AreEqual( 42.ToBigInteger(), vble_y.LiteralValue.ToBigInteger() );
}

[Test]
public void TestPtrPtrExpression()
{
PtrVariable int_ptr = null;
PtrVariable int_ptrptr = null;
PtrVariable int_ptrptrptr = null;
Variable vble_x = null;
Variable vble_y = null;

Assert.DoesNotThrow( () => {
vble_x = this.machine.Execute( @"x = -1" );
vble_y = this.machine.Execute( @"int xx = -1" );
this.machine.Execute( @"ptrInt = &x" );
this.machine.Execute( @"int ** ptrptrInt = &ptrInt" );
this.machine.Execute( @"int *** ptrptrptrInt = &ptrptrInt" );
int_ptrptr = (PtrVariable) this.machine.TDS.LookUp( @"ptrptrInt" );
int_ptr = (PtrVariable) this.machine.TDS.LookUp( @"ptrInt" );
int_ptrptrptr = (PtrVariable) this.machine.TDS.LookUp( @"ptrptrptrInt" );
});

int addrX = (int) vble_x.Address;
int addrPtr = (int) int_ptr.Address;
int addrPtrPtr = (int) int_ptrptr.Address;

int valPtr = (int) int_ptr.LiteralValue.ToBigInteger();
int valPtrPtr = (int) int_ptrptr.LiteralValue.ToBigInteger();
int valPtrPtrPtr = (int) int_ptrptrptr.LiteralValue.ToBigInteger();

Assert.AreEqual( addrX, valPtr );
Assert.AreEqual( addrPtr, valPtrPtr );
Assert.AreEqual( addrPtrPtr, valPtrPtrPtr );

Assert.DoesNotThrow( () => {
this.machine.Execute( @"*ptrInt = 42" );
});

addrX = (int) vble_x.Address;
addrPtr = (int) int_ptr.Address;
addrPtrPtr = (int) int_ptrptr.Address;

valPtr = (int) int_ptr.LiteralValue.ToBigInteger();
valPtrPtr = (int) int_ptrptr.LiteralValue.ToBigInteger();
valPtrPtrPtr = (int) int_ptrptrptr.LiteralValue.ToBigInteger();

Assert.AreEqual( addrX, valPtr );
Assert.AreEqual( addrPtr, valPtrPtr );
Assert.AreEqual( addrPtrPtr, valPtrPtrPtr );
Assert.AreEqual( 42, (int) vble_x.LiteralValue.ToBigInteger() );

Assert.DoesNotThrow( () => {
this.machine.Execute( @"**ptrptrInt = 84" );
});

addrX = (int) vble_x.Address;
addrPtr = (int) int_ptr.Address;
addrPtrPtr = (int) int_ptrptr.Address;

valPtr = (int) int_ptr.LiteralValue.ToBigInteger();
valPtrPtr = (int) int_ptrptr.LiteralValue.ToBigInteger();
valPtrPtrPtr = (int) int_ptrptrptr.LiteralValue.ToBigInteger();

Assert.AreEqual( addrX, valPtr );
Assert.AreEqual( addrPtr, valPtrPtr );
Assert.AreEqual( addrPtrPtr, valPtrPtrPtr );
Assert.AreEqual( 84, (int) vble_x.LiteralValue.ToBigInteger() );

Assert.DoesNotThrow( () => {
this.machine.Execute( @"***ptrptrptrInt = 96" );
});

addrX = (int) vble_x.Address;
addrPtr = (int) int_ptr.Address;
addrPtrPtr = (int) int_ptrptr.Address;

valPtr = (int) int_ptr.LiteralValue.ToBigInteger();
valPtrPtr = (int) int_ptrptr.LiteralValue.ToBigInteger();
valPtrPtrPtr = (int) int_ptrptrptr.LiteralValue.ToBigInteger();

Assert.AreEqual( addrX, valPtr );
Assert.AreEqual( addrPtr, valPtrPtr );
Assert.AreEqual( addrPtrPtr, valPtrPtrPtr );
Assert.AreEqual( 96, (int) vble_x.LiteralValue.ToBigInteger() );

Assert.DoesNotThrow( () => {
this.machine.Execute( @"ptrInt = &xx" );
this.machine.Execute( @"***ptrptrptrInt = 128" );
});

int addrY = (int) vble_y.Address;
addrPtr = (int) int_ptr.Address;
addrPtrPtr = (int) int_ptrptr.Address;

valPtr = (int) int_ptr.LiteralValue.ToBigInteger();
valPtrPtr = (int) int_ptrptr.LiteralValue.ToBigInteger();
valPtrPtrPtr = (int) int_ptrptrptr.LiteralValue.ToBigInteger();

Assert.AreEqual( addrY, valPtr );
Assert.AreEqual( addrPtr, valPtrPtr );
Assert.AreEqual( addrPtrPtr, valPtrPtrPtr );
Assert.AreEqual( 96, (int) vble_x.LiteralValue.ToBigInteger() );
Assert.AreEqual( 128, (int) vble_y.LiteralValue.ToBigInteger() );
}

[Test]
public void TestRefExpression()
{
Expand Down
1 change: 1 addition & 0 deletions CSimTests/TypeTests.cs
@@ -1,3 +1,4 @@
// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using System.Numerics;
Expand Down
3 changes: 2 additions & 1 deletion CSimTests/UInt128Tests.cs
@@ -1,4 +1,5 @@

// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using System;
using System.Numerics;
Expand Down
1 change: 1 addition & 0 deletions CSimTests/VbleTests.cs
@@ -1,3 +1,4 @@
// CSim - (c) 2014-17 Baltasar MIT License <jbgarcia@uvigo.es>

namespace CSimTests {
using System.Numerics;
Expand Down

0 comments on commit ef40413

Please sign in to comment.