fusionlanguage / fut

Fusion programming language. Transpiling to C, C++, C#, D, Java, JavaScript, Python, Swift, TypeScript and OpenCL C.
https://fusion-lang.org
GNU General Public License v3.0
1.76k stars 55 forks source link

void function returns false if throws #37

Closed Chuvi-w closed 3 years ago

Chuvi-w commented 3 years ago

public class CanThrow { public static void WillThrow(int a) { if (a<15){throw "a<15";} Console.WriteLine(a); } }

compiles into void CanThrow_WillThrow(int a) { if (a < 15) { return false; } printf("%d\n", a); }

pfusik commented 3 years ago

This was invalid Ć. The method should have been marked with throws, like in Java:

public class CanThrow { public static void WillThrow(int a) throws { if (a<15){throw "a<15";} Console.WriteLine(a); } }

I've added the missing error message.

C doesn't support exceptions, so they are emulated by magic return values, here false for an exception.

Thank you!

Chuvi-w commented 3 years ago

void funtion must not return anything.

warning: 'return' with a value, in function returning void [-Wreturn-type]

pfusik commented 3 years ago

There were bugs both in your code and cito. Please pull cito and rebuild.

C:\0\ci>cat 37.ci
public class CanThrow { public static void WillThrow(int a) { if (a<15){throw "a<15";} Console.WriteLine(a); } }
C:\0\ci>cito -o 37.c 37.ci
37.ci(1): ERROR: 'throw' in a method not marked 'throws'

C:\0\ci>cat 37-fixed.ci
public class CanThrow { public static void WillThrow(int a) throws { if (a<15){throw "a<15";} Console.WriteLine(a); } }
C:\0\ci>cito -o 37-fixed.c 37-fixed.ci

C:\0\ci>cat 37-fixed.c
// Generated automatically with "cito". Do not edit.
#include <stdio.h>
#include <stdlib.h>
#include "37-fixed.h"

struct CanThrow {
};

bool CanThrow_WillThrow(int a)
{
        if (a < 15) {
                return false;
        }
        printf("%d\n", a);
        return true;
}

C:\0\ci>gcc -c -Wall 37-fixed.c

C:\0\ci>