Commit 93c55a3e authored by Laszlo Domoszlai's avatar Laszlo Domoszlai
Browse files

Function/ADT/Record descriptors are created from precompiled SAPL code now

parent f30e8871
g++ -Wno-write-strings -Wno-pointer-arith main.c fun.c mem.c parse.c -o main
g++ -Wno-write-strings -Wno-pointer-arith main.c desc.c mem.c parse.c -o main
#include "fun.h"
#include "desc.h"
#include <stdio.h>
#include <stdlib.h>
......@@ -20,13 +20,13 @@ KHASH_MAP_INIT_STR(khStrPtr, FunFlags*) // setup khash to handle string key with
// create a hashtable
khash_t(khStrPtr) *funHash = kh_init(khStrPtr);
void add_fun(char* fn, FunFlags* desc)
void add_desc(char* fn, FunFlags* desc)
{
khiter_t k; // used by the macros
kh_set(khStrPtr, funHash, fn, desc);
}
FunFlags* find_fun(char* fn)
FunFlags* find_desc(char* fn)
{
khiter_t k; // used by the macros
return kh_get_val(khStrPtr, funHash, fn);
......
#ifndef __FUN_H
#define __FUN_H
#ifndef __DESC_H
#define __DESC_H
#define FT_RECORD 1
#define FT_ADT 2
......@@ -32,7 +32,7 @@ struct ADTEntry
{
struct FunFlags flags;
int strictness;
char* name;
char name[];
};
struct CAFEntry
......@@ -54,9 +54,10 @@ struct RecordEntry
struct FunFlags flags;
int strictness;
char** fields;
char name[];
};
void add_fun(char* fn, FunFlags* desc);
FunFlags* find_fun(char* fn);
void add_desc(char* fn, FunFlags* desc);
FunFlags* find_desc(char* fn);
#endif // __FUN_H
\ No newline at end of file
#endif // __DESC_H
\ No newline at end of file
......@@ -2,7 +2,7 @@
#include <stdio.h>
#include "mem.h"
#include "fun.h"
#include "desc.h"
#include "parse.h"
int main()
......@@ -10,14 +10,10 @@ int main()
init_mem();
int res;
char* stream = "37 F12 example.g_453 6 AF3 add2 VA0 VA1 ";
char* stream = "40 R10 example._R2 1 9 example.a9 example.b";
FunEntry* entry;
parseLine(&stream, (FunFlags**) &entry);
printf("%s", entry->name);
parseLine1(&stream);
// add_fun("jimbo", (FunFlags*) "artist");
// char *tval = (char*) find_fun("jimbo");
// printf ("shortcut tval for jimbo = %s\n", tval);
......
......@@ -2,32 +2,15 @@
#include "mem.h"
void* code_mem_free_ptr;
void* code_mem_end;
void* fun_table_free_ptr;
void* fun_table_end;
int desc_alloc;
void init_mem()
{
code_mem_free_ptr = malloc(CODEMEMSIZE);
code_mem_end = code_mem_free_ptr + CODEMEMSIZE;
fun_table_free_ptr = malloc(FUNTABLESIZE);
fun_table_end = fun_table_free_ptr + FUNTABLESIZE;
desc_alloc = 0;
}
void* alloc_fun_table(int size)
void* alloc_desc(int size)
{
void* current = fun_table_free_ptr;
fun_table_free_ptr += size;
if(fun_table_free_ptr < fun_table_end)
{
return current;
}
else
{
return 0;
}
desc_alloc += size;
return malloc(size);
}
\ No newline at end of file
#ifndef __MEM_H
#define __MEM_H
#define CODEMEMSIZE 10000
#define FUNTABLESIZE 10000
void init_mem();
// TODO: inline
void* alloc_fun_table(int size);
void* alloc_desc(int size);
#endif // __MEM_H
......@@ -4,7 +4,6 @@
#include <string.h>
#include "parse.h"
#include "fun.h"
#include "mem.h"
int parseInt(char** ptr, int* result)
......@@ -23,7 +22,7 @@ int parseInt(char** ptr, int* result)
}
}
int parseLine(char** ptr, FunFlags** fun)
int parseLine1(char** ptr)
{
int lineSize;
......@@ -37,17 +36,20 @@ int parseLine(char** ptr, FunFlags** fun)
switch(type)
{
case 'F': // Normal function
{
// example.g_45 !a_0 !b_1 c_2 = add a_0 b_1
// 37 F12 example.g_453 6 AF3 add2 VA0 VA1
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
FunEntry* entry = (FunEntry*) alloc_fun_table(sizeof(FunEntry) + nameLength + 1);
FunEntry* entry = (FunEntry*) alloc_desc(sizeof(FunEntry) + nameLength + 1);
entry->flags.type = FT_FUN;
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity;
......@@ -58,9 +60,80 @@ int parseLine(char** ptr, FunFlags** fun)
// set the continuation for stage 2
entry->body = *ptr;
*fun = (FunFlags*) entry;
add_desc(entry->name, (FunFlags*) entry);
break;
}
case 'A': // ADT
{
// :: example.A = example.A !a1 a2 | example.B
// 33 A2 9 example.A2 2 9 example.B0 0
int conNum;
if(!parseInt(ptr, &conNum)) return 0;
for(int i=0; i<conNum; i++)
{
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
ADTEntry* entry = (ADTEntry*) alloc_desc(sizeof(ADTEntry) + nameLength + 1);
entry->flags.type = FT_ADT;
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0;
add_desc(entry->name, (FunFlags*) entry);
}
break;
}
case 'R': // Record
{
// :: example._R = {example.a, !example.b}
// 40 R10 example._R2 1 9 example.a9 example.b
int nameLength;
if(!parseInt(ptr, &nameLength)) return 0;
RecordEntry* entry = (RecordEntry*) alloc_desc(sizeof(RecordEntry) + nameLength + 1);
entry->flags.type = FT_RECORD;
memcpy(entry->name, *ptr, nameLength);
entry->name[nameLength] = '\0';
*ptr += nameLength;
int arity;
if(!parseInt(ptr, &arity)) return 0;
entry->flags.arity = arity;
if(!parseInt(ptr, &entry->strictness)) return 0;
entry->fields = (char**) alloc_desc(sizeof(char*) * arity);
for(int i=0; i<arity; i++)
{
int fieldNameLength;
if(!parseInt(ptr, &fieldNameLength)) return 0;
entry->fields[i] = (char*) malloc(fieldNameLength);
memcpy(entry->fields[i], *ptr, fieldNameLength);
entry->fields[i][fieldNameLength] = '\0';
*ptr += fieldNameLength;
}
add_desc(entry->name, (FunFlags*) entry);
break;
}
default:
return 0;
}
......
#ifndef __PARSE_H
#define __PARSE_H
#include "fun.h"
#include "desc.h"
//int parseInt(char** str, int* result);
int parseLine(char** ptr, FunFlags** fun);
int parseLine1(char** ptr);
#endif // __PARSE_H
\ No newline at end of file
module parsercompiler
module precompiler
import Sapl.SaplParser
import Sapl.SaplTokenizer
......@@ -30,11 +30,19 @@ sFunc ctx (FTFunc name body params) a
# ctx = {ctx & vars = addVars ctx.vars 0 params}
= a <++ "F" <++ sText (unpackVar name) <++ sNum (length params) <++ sNum (calcStrictness params) <++ sTerm ctx body
sList f es a = a <++ sNum (length es) <++ sList0 es
where
sList0 [e] a = a <++ f e
sList0 [e:es] a = sList0 es (a <++ f e <++ "")
sList0 [] a = a
sFunc ctx (FTRecord name fields) a
= a <++ "R" <++ sText (unpackVar name) <++ sNum (length fields) <++ sNum (calcStrictness fields) <++ sList0 sText (map unpackVar fields)
sFunc ctx (FTADT typeName cs) a
= a <++ "A" <++ sList sCon cs
where
sCon (SaplConstructor name _ params) a
= a <++ sText (unpackVar name) <++ sNum (length params) <++ sNum (calcStrictness params)
sList f es a = a <++ sNum (length es) <++ sList0 f es
sList0 f [e] a = a <++ f e
sList0 f [e:es] a = sList0 f es (a <++ f e <++ "")
sList0 f [] a = a
sNum num a = a <++ num <++ " "
sText text a = a <++ sNum (textSize text) <++ text
......
......@@ -2,7 +2,7 @@ Version: 1.4
Global
ProjectRoot: .
Target: iTasks
Exec: {Project}\parsercompiler.exe
Exec: {Project}\precompiler.exe
CodeGen
CheckStacks: False
CheckIndexes: True
......@@ -46,7 +46,7 @@ Global
Precompile:
Postlink:
MainModule
Name: parsercompiler
Name: precompiler
Dir: {Project}
Compiler
NeverMemoryProfile: False
......@@ -60,62 +60,6 @@ MainModule
ReuseUniqueNodes: True
Fusion: False
OtherModules
Module
Name: Sapl.FastString
Dir: {Project}
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.SaplParser
Dir: {Project}
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.SaplStruct
Dir: {Project}
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.SaplTokenizer
Dir: {Project}
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: StdArray
Dir: {Application}\Libraries\StdEnv
......@@ -851,6 +795,62 @@ OtherModules
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.FastString
Dir: {Application}\iTasks-SDK\Dependencies\SAPL
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.SaplParser
Dir: {Application}\iTasks-SDK\Dependencies\SAPL
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.SaplStruct
Dir: {Application}\iTasks-SDK\Dependencies\SAPL
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: Sapl.SaplTokenizer
Dir: {Application}\iTasks-SDK\Dependencies\SAPL
Compiler
NeverMemoryProfile: False
NeverTimeProfile: False
StrictnessAnalysis: True
ListTypes: StrictExportTypes
ListAttributes: True
Warnings: True
Verbose: True
ReadableABC: False
ReuseUniqueNodes: True
Fusion: False
Module
Name: _SystemDynamic
Dir: {Application}\iTasks-SDK\Patches\Dynamics
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment