Skip to the content.

struixCC Coding Guide

Table of Contents


Introduction

struixCC is a compiler that translates a subset of the C programming language into struixLang, a stack-based programming language. struixCC allows developers to write code using familiar C syntax and compiles it into struixLang code, which can then be executed within the struixLang interpreter.

This guide provides an overview of struixCC, including supported features, limitations, and helpful tips for programmers. It aims to assist developers in effectively using struixCC to write and compile C code for execution in the struixLang environment.


Getting Started

Installation

To use struixCC, ensure you have the following prerequisites:

Compiling C Code to struixLang

  1. Write Your C Code: Create a C source file (example.c) using the supported features of struixC.

  2. Compile Using struixCC:

    Run the struixCC compiler to translate your C code into struixLang code:

    python struixCC.py example.c -o example.sx
    

    This command generates an output file example.sx containing the equivalent struixLang code.

  3. Run the struixLang Code:

    Use the struixLang interpreter to execute the compiled code:

    python struixTerp.py example.sx
    

Supported Features

struixCC supports a subset of the C programming language. Below are the key features and constructs that you can use.

Data Types

Variables and Declarations

Operators

Control Structures

Functions

Arrays


Limitations and Differences from Standard C

struixCC aims to provide a subset of C features suitable for compilation into struixLang. Below are the limitations and differences compared to standard C on GCC/Linux.

Unsupported Features

Scoping and Variable Lifetime

Standard Libraries


Helpful Features and Tips

Debugging

Optimizing Code

Working with Arrays

Control Flow Constructs


Examples

Hello, World

int main() {
  // Since printf is not available, we'll use struixLang's PRINT after compilation
  return 0;
}

Note: To display output, you can modify the generated struixLang code to include the PRINT word or use struixLang’s features directly.

Factorial Function

int factorial(int n) {
  int result = 1;
  int i;
  for (i = 1; i <= n; i++) {
    result = result * i;
  }
  return result;
}

int main() {
  int number = 5;
  int fact = factorial(number);
  // Display the result using struixLang's PRINT after compilation
  return 0;
}

Array Manipulation

int main() {
  int arr[5];
  int i;
  for (i = 0; i < 5; i++) {
    arr[i] = i * 2;
  }
  // Access and manipulate array elements
  return 0;
}

Control Flow Example

int main() {
  int x = 10;
  if (x > 5) {
    x = x + 1;
  } else {
    x = x - 1;
  }

  switch (x) {
    case 10:
      // Do something
      break;
    case 11:
      // Do something else
      break;
    default:
      // Default case
      break;
  }
  return 0;
}

Conclusion

struixCC offers a way to write programs using familiar C syntax and compile them into struixLang for execution in a stack-based environment. While it supports many fundamental features of C, there are limitations to consider.

When working with struixCC:

This guide serves as a starting point for developers looking to use struixCC. By keeping the limitations and differences in mind, you can effectively write and compile code for the struixLang interpreter.

Happy coding with struixCC!