Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

String subscript in C# should be converted to int #19

Open
dubek opened this issue Nov 20, 2016 · 1 comment
Open

String subscript in C# should be converted to int #19

dubek opened this issue Nov 20, 2016 · 1 comment

Comments

@dubek
Copy link

dubek commented Nov 20, 2016

Consider this Skew program:

@entry
def main {
  var s = "ABCDEF"
  switch s[0] {
    case 'A' { assert(true) }
    default { assert(false) }
  }
}

It compiles to JS and runs OK (no assertion). However, when compiling to C# and then to exe, the Mono compiler mcs yells:

t.cs(16,18): error CS0031: Constant value `65' cannot be converted to a `char'
Compilation failed: 1 error(s), 0 warnings

The resulting C# program is:

using System.Diagnostics;

public class Globals
{
    public static void assert(bool truth)
    {
        Debug.Assert(truth);
    }

    public static void Main()
    {
        string s = "ABCDEF";

        switch (s[0])
        {
            case 65:
            {
                Globals.assert(true);
                break;
            }

            default:
            {
                Globals.assert(false);
                break;
            }
        }
    }
}

The problem is that s[0] in C# returns a char; inside the case statement this is not automatically casted to int (whereas in == expressions it is). Maybe instead of s[0] skewc should emit something along the lines of Javascript's s.charCodeAt(0) (I don't know C#, sorry).

dubek added a commit to dubek/mal that referenced this issue Nov 20, 2016
See http://skew-lang.org/ for details on the Skew language. Currently
Mal only compiles to Javascript, as there are some issues with the C#
backend for Skew (evanw/skew#19).

Tested with Skew 0.7.42.
dubek added a commit to dubek/mal that referenced this issue Nov 20, 2016
See http://skew-lang.org/ for details on the Skew language. Currently
Mal only compiles to Javascript, as there are some issues with the C#
backend for Skew (evanw/skew#19).

Tested with Skew 0.7.42.
dubek added a commit to dubek/mal that referenced this issue Nov 20, 2016
See http://skew-lang.org/ for details on the Skew language. Currently
Mal only compiles to Javascript, as there are some issues with the C#
backend for Skew (evanw/skew#19).

Tested with Skew 0.7.42.
@mingodad
Copy link

Trying to understand how skew code generation works I did this change to csharp.sk:

        case .SWITCH {
          var switchValue = node.switchValue
          _emit(_indent + "switch (")
	  if switchValue.kind == .INDEX {
		_emit("(int)")
	  }
          _emitExpression(switchValue, .LOWEST)
          _emit(")\n" + _indent + "{\n")
          _increaseIndent

But probably it also need to cast in other places a string index is used (like in comparisons and assignments).

micfan pushed a commit to micfan/make-a-lisp that referenced this issue Nov 8, 2018
See http://skew-lang.org/ for details on the Skew language. Currently
Mal only compiles to Javascript, as there are some issues with the C#
backend for Skew (evanw/skew#19).

Tested with Skew 0.7.42.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants