What programs can translate C to freebasic?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
lassar
Posts: 306
Joined: Jan 17, 2006 1:35

What programs can translate C to freebasic?

Post by lassar »

With so much C code out there, has anyone done a C to freebasic translator?
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: What programs can translate C to freebasic?

Post by badidea »

Not that I am aware of. There are tools for c-header conversion (https://freebasic.net/forum/viewtopic.p ... er#p243278). Also it is possible to link a compiled c-file with FreeBASIC code. A full C to FreeBASIC translator sounds like a lot of work. Basically writing a C code interpreter. Doesn't sound like fun.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: What programs can translate C to freebasic?

Post by anonymous1337 »

badidea:

We have FreeBASIC that can run in the web browser / to JavaScript. We have FreeBASIC to C and LLVM. The FB runtime is written in C (and C++?)

C is a simple language. I'm honestly surprised this isn't a thing yet :)
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: What programs can translate C to freebasic?

Post by srvaldez »

anonymous1337 wrote: C is a simple language.
the problem I find with C are on-the-fly assignments, specially within conditionals, also the for-loops can be very cryptic.

Code: Select all

if (!(m*=2)) m = 2;
however, FBfrog does a good job at header translation, if it would handle for-loops it would almost fulfill the OP's request.
for small snippets of C code that have for-loops, one can comment-out the for-expression and then feed that to FBfrog which will then translate the statements within the loop encapsulated in scope/end scope statements which must be replaced with the loop.
there's a prickly problem if the C code has nested functions with many shared variables, there simply is no straight-forward way of translation to FB.
[edit] there's a C++ to VB Converter, it's a commercial product and it's not perfect, you can use it for free for small snippets.
Last edited by srvaldez on Aug 19, 2018 11:30, edited 4 times in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: What programs can translate C to freebasic?

Post by caseih »

srvaldez wrote:the problem I find with C are on-the-fly assignments, specially within conditionals, also the for-loops can be very cryptic.
Fortunately they need not be cryptic to a parser that understands the grammar of a C expression.
there's a prickly problem if the C code has nested functions with many shared variables, there simply is no straight-forward way of translation to FB.
C does not have nested functions, so fortunately that won't be a problem. I'm not sure what you are talking about with the phrase "many shared variables" though.

The actual things that make translating C into FB very difficult is not the C language itself, but the preparser. This is the biggest weakness in programs like FBFrog. Header files typically have a lot of macro meta language in them, and lots of references to macros all throughout. Really any translation program has to essentially be a C preprocessor and also compiler. Compiler writing, even for a simple language like C, is quite a project. Translating C to FB involves making a compiler. This could be a fun and educational task, but it wouldn't be trivial!
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: What programs can translate C to freebasic?

Post by marcov »

anonymous1337 wrote: C is a simple language. I'm honestly surprised this isn't a thing yet :)
The core language maybe, but not if you include the preprocessor . You can insert and craft random C syntax with it, needing the target language of any translator to nearly fully implement C and its syntax for a transparent result.

This is why header and C translators will always remain manual tools with the results needing manual modification. (or better: proper manual instrumentation of the translator)

(added later:) And preprocessor use is not exactly rare in C
Last edited by marcov on Aug 20, 2018 9:22, edited 1 time in total.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: What programs can translate C to freebasic?

Post by srvaldez »

unfortunately gnu gcc supports nested functions as an extension and some people use them, I tried to convert this code to FB https://www.rosettacode.org/wiki/Word_wrap#C
complete code at the link above, notice the function test_wrap inside the function balanced_wrap

Code: Select all

int balanced_wrap(word words, int count, int cols, int *breaks)
{
	int *best = malloc(sizeof(int) * (count + 1));
 
	/* do a greedy wrap to have some baseline score to work with, else
	   we'll end up with O(2^N) behavior */
	int best_score = greedy_wrap(words, count, cols, breaks);
 
	void test_wrap(int line_no, int start, int score) {
		int line = 0, current_score = -1, d;
 
		while (start <= count) {
			if (line) line ++;
			line += words[start++].len;
			d = cols - line;
			if (start < count || d < 0) {
				if (d > 0)
					current_score = score + PENALTY_SHORT * d * d;
				else
					current_score = score + PENALTY_LONG * d * d;
			} else {
				current_score = score;
			}
 
			if (current_score >= best_score) {
				if (d <= 0) return;
				continue;
			}
 
			best[line_no] = start;
			test_wrap(line_no + 1, start, current_score);
		}
		if (current_score >= 0 && current_score < best_score) {
			best_score = current_score;
			memcpy(breaks, best, sizeof(int) * (line_no));
		}
	}
	test_wrap(0, 0, 0);
	free(best);
 
	return best_score;
}
Last edited by srvaldez on Aug 19, 2018 22:13, edited 1 time in total.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: What programs can translate C to freebasic?

Post by paul doe »

srvaldez wrote:unfortunately gcc supports nested functions as an extension and some people use them...
Some members here use a #macro inside the function to pose as a nested function. Or, you can wrap the two functions inside a class (if you like/prefer the OOP approach).
srvaldez wrote:... I tried to convert this code to FB https://www.rosettacode.org/wiki/Word_wrap#C
complete code at the link above, notice the function test_wrap inside the function balanced_wrap
Sometimes I find it easier to translate C++ than C (too low level), but unfortunately, FB is too barebones and more often than not you need to implement some functionality yourself (like the std::stream classes). This code snippet is a port of the C++ code that's immediately below the one you're translating:

Code: Select all

type ExtString
  public:
    declare constructor()
    declare constructor( byref as ExtString )
    declare constructor( byref as const string )
    declare operator let( byref as ExtString )
    declare operator let( byref as const string )
    declare operator cast() as string
    
    declare destructor()
    
    declare property length() as uinteger
    declare sub add( byref as const string )
    declare function remove() as string

  private:
    m_string as string
end type

constructor ExtString()
end constructor

constructor ExtString( byref aString as const string )
  m_string = aString
end constructor

constructor ExtString( byref rhs as ExtString )
  m_string = rhs.m_string
end constructor

operator ExtString.let( byref rhs as ExtString )
  m_string = rhs.m_string
end operator

operator ExtString.let( byref rhs as const string )
  m_string = rhs
end operator

operator ExtString.cast() as string
  return( m_string )
end operator

destructor ExtString()
end destructor

property ExtString.length() as uinteger
  return( len( m_string ) )
end property

sub ExtString.add( byref aString as const string )
  m_string &= aString
end sub

function ExtString.remove() as string
  dim as const string wordSeparators = _
    chr( 13, 10 ) & chr( 32 ) & chr( 9 )
    
  dim as integer nextWord = inStr( _
    m_string, any wordSeparators )
  
  dim as string ret
  
  if( nextWord = 0 ) then
    ret = m_string
    m_string = ""
  else
    ret = mid( m_string, 1, nextWord - 1 )
    m_string = mid( _
      m_string, nextWord + 1, len( m_string ) - nextWord )
  end if
  
  return( ret ) 
end function

function wrappedString( _
  byref text as const string, _
  byval lineLength as integer = 72 ) as string
  
  dim as ExtString words = text
  dim as ExtString wrapped
  dim as string word
  
  if( words.length > 0 ) then
    word = words.remove()
    wrapped.add( word )
    
    dim as integer spaceLeft = lineLength - len( word )
    
    do while( words.length > 0 )
      word = words.remove()
      
      if( spaceLeft < len( word ) + 1 ) then
        wrapped.add( chr( 13, 10 ) & word )
        spaceLeft = lineLength - len( word )
      else
        wrapped.add( " " & word )
        spaceLeft -= len( word ) + 1
      end if
    loop
  end if
  
  return( wrapped )
end function

dim as string text = _
  "In olden times when wishing still helped one, there lived a king " & _
  "whose daughters were all beautiful, but the youngest was so beautiful " & _
  "that   the sun itself, which has seen so much, was astonished whenever " & _
  "it shone in her face.  Close by the king's castle lay a great dark " & _
  "forest, and under an old lime tree in the forest was a well, and when " & _
  "the day was very warm, the king's child went out into the forest and " & _
  "sat down by the side of the cool fountain, and when she was bored she " & _
  "took a golden ball, and threw it up on high and caught it, and this " & _
  "ball was her favorite plaything."

dim as ExtString es = text
dim as string s = wrappedString( text, 40 )

? s

sleep()
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: What programs can translate C to freebasic?

Post by anonymous1337 »

marcov,

I forgot about the preprocessor. Whoops.
Post Reply