Desafio - Compilador (Inventem uma liguagem)

Started by KTachyon, May 04, 2006, 03:44:16 AM

Previous topic - Next topic

KTachyon

Estou a criar um repositório para este projecto no SourceForge.net. Depois vou fazendo updates acerca do projecto neste tópico.
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

Segundo as normas do SourceForge, o projecto será Open Source, tendo obrigatoriamente que seguir as seguintes normas (passo a citar):

QuoteThe Open Source Definition

Introduction

Open source doesn\\'t just mean access to the source code. The distribution terms of open-source software must comply with the following criteria:


1. Free Redistribution

The license shall not restrict any party from selling or giving away the software as a component of an aggregate software distribution containing programs from several different sources. The license shall not require a royalty or other fee for such sale.

2. Source Code

The program must include source code, and must allow distribution in source code as well as compiled form. Where some form of a product is not distributed with source code, there must be a well-publicized means of obtaining the source code for no more than a reasonable reproduction cost preferably, downloading via the Internet without charge. The source code must be the preferred form in which a programmer would modify the program. Deliberately obfuscated source code is not allowed. Intermediate forms such as the output of a preprocessor or translator are not allowed.

3. Derived Works

The license must allow modifications and derived works, and must allow them to be distributed under the same terms as the license of the original software.

4. Integrity of The Author\\'s Source Code

The license may restrict source-code from being distributed in modified form only if the license allows the distribution of \\"patch files\\" with the source code for the purpose of modifying the program at build time. The license must explicitly permit distribution of software built from modified source code. The license may require derived works to carry a different name or version number from the original software.

5. No Discrimination Against Persons or Groups

The license must not discriminate against any person or group of persons.

6. No Discrimination Against Fields of Endeavor

The license must not restrict anyone from making use of the program in a specific field of endeavor. For example, it may not restrict the program from being used in a business, or from being used for genetic research.

7. Distribution of License

The rights attached to the program must apply to all to whom the program is redistributed without the need for execution of an additional license by those parties.

8. License Must Not Be Specific to a Product

The rights attached to the program must not depend on the program\\'s being part of a particular software distribution. If the program is extracted from that distribution and used or distributed within the terms of the program\\'s license, all parties to whom the program is redistributed should have the same rights as those that are granted in conjunction with the original software distribution.

9. License Must Not Restrict Other Software

The license must not place restrictions on other software that is distributed along with the licensed software. For example, the license must not insist that all other programs distributed on the same medium must be open-source software.

*10. License Must Be Technology-Neutral

No provision of the license may be predicated on any individual technology or style of interface.

Espero k concordem com isto... podem opinar acerca disto.

Depois ainda vou ter k escolher uma mais específica.
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

E a licença escolhida foi... GPL, lol. Hope you like it.
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

Teotónio Ricardo

KhAoTiK_TaChYoN boas escolhas.
Precisamos de um nome para o projecto antes disso...

Ghetto_Smurf

concordo com a GPL, não é muito restricta e não é tão livre como a BSD

KTachyon

Eu dei-lhe o nome de WebTugaPL (WebTuga Programming Language). Agora estou à espera que eles aceitem o projecto e me forneçam o espaço.
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

Teotónio Ricardo

QuoteEu dei-lhe o nome de WebTugaPL (WebTuga Programming Language). Agora estou à espera que eles aceitem o projecto e me forneçam o espaço.

Depois vamos criar uma página com apresentação digna do Webtuga.
Acho o nome bem escolhido.

KTachyon

My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

WebTuga Programming Language Compiler v0.01a released

Apenas código fonte. Nem sequer é compilável. Alguém para fazer a webpage?
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

Teotónio Ricardo

Eu, é só me dares os dados que eu faço...

KTachyon

Cria uma conta no SourceForge.net e depois dá-m o teu username para eu te adicionar à equipa.
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

Ok, isto vai indo mas d-e-v-a-g-a-r... Estive a ver akilo que já fiz com o código e achei a linguagem um pouco vazia. Penso que vou adicionar um tipo de variável boleana, que pode ter um dos seguintes valores:

sim ou verdadeiro
não ou falso

Depois, ainda tenho que pensar como aceito as condições de teste dos ifs e ciclos. E ainda faltam as operações sobre strings... Ideias?
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

WPL.l - June 13, 2006

%{

#include "y.tab.h"

#include

#include

%}



%%

"//".*n {/*line++*/;}



"-"?[0-9]+ {yylval.inteiro = atoi(yytext); return INTEGER;}

"-"?[0-9]+"."[0-9]+ {yylval.nint = atof(yytext); return FLOAT;}

""".""" {yylval.str = (char*) strdup(yytext);}



"@"[a-zA-Z0-9_]* {yylval.var = (char*) strdup(yytext); return VAR;}



"SE" {return SE;}

"OU SE" {return OU_SE;}

"SENÃÆ'O" {return SENAO;}



"DEFINE" {return DEFINE;}

"COM" {return COM;}

"RETORNA" {return RETORNA;}



"FAZ" {return FAZ;}

"ATÉ" {return ATE;}

"REPETE" {return REPETE;}

"ENQUANTO" {return ENQUANTO;}



"=" {return IGUAL;}

">" {return MAIOR;}

"<" {return MENOR;}

"<=" {return MENOR_IGUAL;}

">=" {return MAIOR_IGUAL;}



"+" {return SOMA;}

"-" {return SUBT;}

"*" {return MULT;}

"/" {return DIVI;}



[a-zA-Z][a-zA-Z0-9_]* {yylval.function = (char*) strdup(yytext); return FUNCTION;}



[ t] ;



n {/*line++*/;}



. return yytext[0];

%%

int yywrap()

{

return 1;

}
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

WPL.y - June 13, 2006

%{

#include

#include

%}

%token INTEGER

%token FLOAT

%token STRING

%token VAR

%token FUNCTION



%token SE

%token OU_SE

%token SENAO



%token DEFINE

%token COM

%token RETORNA



%token FAZ

%token ATE

%token REPETE

%token ENQUANTO



%token IGUAL

%token MAIOR

%token MENOR

%token MENOR_IGUAL

%token MAIOR_IGUAL



%token SOMA

%token SUBT

%token MULT

%token DIVI



%union{

int inteiro;

char *str;

float nint;

char *var;

char *function;

}



%%



// statement? codelines? são a mm m****...

statement: codeline

| statement codeline

;



// vírgulas? hmm...

codelines: codeline

| codelines codeline

;



// são todas terminadas com \\'.\\'?

codeline: function_if

| function_defun

| function_call

| attrib

| cicle_until

| cicle_while

| cicle_dotimes

;



attrib: VAR IGUAL returnable_type

;



operacao: operacao oper simple_type

| simple_type oper simple_type

;



oper: SOMA

| SUBT

| MULT

| DIVI

;



function_if: SE testcond \\':\\' codelines \\'.\\'

  | SE testcond \\':\\' codelines \\'.\\' cicle_else_if

  | SE testcond \\':\\' codelines \\'.\\' cicle_else_if SENAO \\':\\' codelines \\'.\\'

  | SE testcond \\':\\' codelines \\'.\\' SENAO \\':\\' codelines \\'.\\'

 ;



cicle_else_if: OU_SE testcond \\':\\' codelines \\'.\\'

| cicle_else_if OU_SE testcond \\':\\' codelines \\'.\\'

;



testcond: simple_type

| simple_type test_oper simple_type

;



simple_type: INTEGER

  | FLOAT

  | VAR

 ;



returnable_type: INTEGER

  | FLOAT

  | VAR

  | STRING

  | operacao

  | function_call

 ;



test_oper: IGUAL

| MAIOR

| MENOR

| MAIOR_IGUAL

| MENOR_IGUAL

;



function_defun: DEFINE FUNCTION COM arg_list \\':\\' function_def \\'.\\'

 | DEFINE FUNCTION \\':\\' function_def \\'.\\'

;



function_def: codelines

| RETORNA returnable_type

| codelines \\',\\' RETORNA returnable_type

;



arg_list: VAR

| arg_list \\',\\' VAR

;



function_call: FUNCTION COM call_arg_list \\'.\\'

| FUNCTION \\'.\\'

;



call_arg_list: returnable_type

| call_arg_list \\',\\' returnable_type

;



cicle_until: FAZ codelines ATE testcond \\'.\\'

 ;



cicle_while: ENQUANTO testcond \\':\\' codelines \\'.\\'

 ;



cicle_dotimes: REPETE INTEGER \\':\\' codelines \\'.\\'

;





%%

int main()

{

//line = 1;



yyparse();

}



void yyerror (char *s)

{

printf("%sn", s);

}
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

KTachyon

structures.h - June 13, 2006




typedef struct _a1 is_statement;

typedef struct _a2 is_codeline_list;

typedef struct _a3 is_codeline;

typedef struct _a4 is_attrib;

typedef struct _a5 is_operacao;

typedef struct _a6 is_function_if;

typedef struct _a7 is_cicle_else_if;

typedef struct _a8 is_testcond;

typedef struct _a9 is_simple_type;

typedef struct _a10 is_returnable_type;

typedef struct _a11 is_function_defun;

typedef struct _a12 is_arg_list;

typedef struct _a13 is_function_def;

typedef struct _a14 is_function_call;

typedef struct _a15 is_call_arg_list;

typedef struct _a16 is_cicle_until;

typedef struct _a17 is_cicle_while;

typedef struct _a18 is_cicle_dotimes;



typedef enum {d_function_if, d_function_defun, d_function_call, d_attrib, d_cicle_until, d_cicle_while, d_cicle_dotimes} d_codeline;

typedef enum {d_SOMA, d_SUBT, d_MULT, d_DIVI} d_oper;

typedef enum {d_INTEGER, d_FLOAT, d_VAR} d_simple_type;

typedef enum {d_INTEGER_ret, d_FLOAT_ret, d_VAR_ret, d_STRING, d_operacao, d_function_call_ret} d_returnable_type;

typedef enum {d_IGUAL, d_MAIOR, d_MENOR, d_MAIOR_IGUAL, d_MENOR_IGUAL} d_test_oper;



struct _a1 {

is_codeline *icl;

struct _a1 *next;

}



struct _a2 {

is_codeline *icl;

struct _a1 *next;

}



struct _a3 {

d_codeline type;



union {

is_function_if *ifi;

is_function_defun *ifd;

is_function_call *ifc;

is_attrib *ia;

is_cicle_until *icu;

is_cicle_while *icw;

is_cicle_dotimes *icd;

}

}



struct _a4 {

char *var;

ir_returnable_type *irt;

}



struct _a5 {

is_simple_type *istInit;

is_oper io;

is_simple_type *ist;

is_operacao *next;

}



struct _a6 {

is_testcond *it;

is_codeline_list *icl;

is_cicle_else_if *icei;

is_codeline_list *iclElse;

}



struct _a7 {

is_testcond *it;

is_codeline_list *icl;

is_cicle_else_if *next;

}



struct _a8 {

is_simple_type *ist;

d_test_oper to;

is_simple_type *istComp;

}



struct _a9 {

d_simple_type type;



union {

int integer;

float floating;

char *var;

}

}



struct _a10 {

d_returnable_type type;



union {

int integer;

float floating;

char *var;

char *string;

is_operacao *io;

is_function_call *ifc;

}

}



struct _a11 {

char *function;

is_arg_list *ial;

is_function_def *ifd;

}



struct _a12 {

char *var;

struct _a12 *next;

}



struct _a13 {

is_codeline_list *icl;

is_returnable_type *irt;

}



struct _a14 {

char *function;

is_call_arg_list *ial;

}



struct _a15 {

is_returnable_type *irt;

struct _a15 *next;

}



struct _a16 {

is_codeline_list *icl;

is_testcond *it;

}



// really needed? _a16 and _a17 are all but the same...

struct _a17 {

is_testcond *it;

is_codeline_list *icl;

}



struct _a18 {

int *integer;

is_codeline_list *icl;

}
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

kingless

Junta o código todo e mostra quando estiver acabado, a funcionar e compilavel.

XTK

wow isto ta a andar bem!  se puder ajudar em alguma coisa digam!

KTachyon

Pois... o problema é k afinal que está a inventar a linguagem toda parece k estou a ser eu, visto que encontro muitas falhas que precisam de ser preenchidas...

Ainda falta muito código para completar a linguagem. Depois de finalmente acabar de remendar a gramática da linguagem e análise sintáctica, falta fazer as inserções de dados nas estruturas, análise semântica e só depois é que se começa a pensar em converter o código.

O código já é compilável, só que apenas verifica se as instruções estão bem escritas. Para compilar:


lex WPL.l

yacc -d WPL.y

gcc -o WPL y.tab.c lex.yy.c -ll -ly
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32

dobone

mas tipo, essa linguagem serveria apenas para programas ? ou só web design ? ou os dois ? programa e web design

KTachyon

Apenas linha de comandos. Depois é possível implementar a programação de janelas e programação gráfica, mas deve apenas ser implementada numa fase em que o projecto se mostre minimamente maduro.
My Q4 estimate: Macs: 5.89M; iPods: 4.19M; iPhones: 30.0M; iPads: 18.05M; Revenue: 37.00B; EPS: 9.50
AAPL Q3 result: Macs: 4.02M; iPods: 6.80M; iPhones: 26.0M; iPads: 17.04M; Revenue: 35.02B; EPS: 9.32