naru.h

Go to the documentation of this file.
00001 /*  library to help you to define a type system for a compiler
00002     Copyright (C) 2008  Patrick Frey
00003 
00004     This program is free software: you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation, either version 3 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00018 /*
00019     libnaru is a library that supports you to define a type system for a 
00020     programming language. It is thought to be another lego piece beside 
00021     lexical and syntax analysis in a compiler project. 
00022     The public addressed by libnaru are people that already have some 
00023     experience in mapping of programming languages. 
00024     Libnaru introduces a notion of scope that is part of all definitions
00025     and queries. You define your type system by type identifiers, deduction
00026     rules from one type to another and tuples of types. 
00027     The resolving of expressions is seen as finding the unifier of your 
00028     query expression to any defined type tuple with minimal costs. The costs 
00029     of a unifier is the sum of costs of the type deduction rules used for it. 
00030     The expensive minimal costs path search is doable with reasonable 
00031     performance under the assumption that the first types of a tuple are 
00032     reducing the search space a lot. If the type system of your source 
00033     language is mappable in this form then things like multiple class or 
00034     interface inheritance, global and local namespaces, privilege levels 
00035     of access and even generics (though recursion needed in a meta 
00036     language has to be provided outside) come at your hands.
00037     The name libnaru comes from the Finish word "naru" that means string. 
00038     The reason for this choice is that the results of a resolved expression
00039     are sequences of constructor functors you defined with your type, rule
00040     and tuple definitions. You build the mapped result of the expression 
00041     in the target language by iterating over the elements of these strings 
00042     and doing some mapping defined by the visited constructors.
00043 */
00044 
00045 /*
00046 *   Author:  Patrick Frey
00047 *   Address: Korneliusstr. 11 8008 Zurich Switzerland
00048 *   My mail at yahoo is "patrick p frey"
00049 */
00050 #ifndef NARU_H
00051 #define NARU_H
00052 #ifdef __cplusplus
00053 extern "C" {
00054 #endif
00055 #include <stdint.h>
00056 
00060 typedef int nu_Scope;
00061 
00065 #define nu_Scope_NULL  -1
00066 
00070 typedef struct nu_TypeSystemStruct* nu_TypeSystem;
00071 
00075 typedef int nu_TypeId;
00076 
00080 #define nu_TypeId_NULL  -1
00081 
00085 typedef enum {
00086    NU_OK=0,                           
00087    NU_ERROR_NOMEM=1,                  
00088    NU_ERROR_CONFLICT_RULE=2,          
00089    NU_ERROR_CONFLICT_DECISION=3,      
00090    NU_ERROR_CONFLICT_DUPLICATE_DEF=4, 
00091    NU_ERROR_SYNTAX=5                  
00092 } nu_ErrorCode;
00093 
00097 nu_TypeSystem nu_createTypeSystem(void);
00098 
00102 void nu_destroyTypeSystem( nu_TypeSystem ts);
00103 
00104 
00112 nu_Scope nu_beginScope( nu_TypeSystem ts);
00113 
00124 nu_Scope nu_declareScope( nu_TypeSystem ts);
00125 
00132 int nu_endScope( nu_TypeSystem ts);
00133 
00144 int nu_cmpScope( nu_TypeSystem ts, nu_Scope A, nu_Scope B); 
00145 
00156 int nu_sameScope( nu_TypeSystem ts, nu_Scope A, nu_Scope B);
00157 
00165 int nu_scopeId( nu_TypeSystem ts, nu_Scope scope);
00166 
00167 
00181 nu_TypeId nu_declareType( nu_TypeSystem ts, nu_Scope scope, const char* id, uintptr_t tc, uintptr_t tcarg, nu_ErrorCode* errcode);
00182 
00193 nu_TypeId nu_getType( nu_TypeSystem ts, nu_Scope scope, const char* id);
00194 
00202 char* nu_getTypeName( nu_TypeSystem ts, nu_TypeId type);
00203 
00212 uintptr_t nu_getTypeConstructor( nu_TypeSystem ts, nu_TypeId type, uintptr_t* tcarg);
00213 
00232 int nu_declareRule( nu_TypeSystem ts, nu_Scope scope, nu_TypeId src, nu_TypeId dest, unsigned int cstord, unsigned int cst, uintptr_t tc, uintptr_t tcarg, nu_ErrorCode* errcode); 
00233 
00249 int nu_declareExpression( nu_TypeSystem ts, nu_Scope scope, unsigned int nof_tps, nu_TypeId* tps, nu_TypeId restype, uintptr_t tc, uintptr_t tcarg, nu_ErrorCode* errcode);  
00250 
00268 nu_TypeId nu_getExpressionType( nu_TypeSystem ts, nu_Scope scope, unsigned int nof_tps, nu_TypeId* tps, uintptr_t* tc, uintptr_t* tcarg);
00269 
00294 int nu_resolveExpression( nu_TypeSystem ts, nu_Scope scope, unsigned int nof_tps, nu_TypeId* tps, nu_TypeId* resulttype, nu_ErrorCode* errcode);
00295 
00308 int nu_getRuleConflict( nu_TypeSystem ts, nu_TypeId** path1, nu_TypeId** path2);
00309 
00320 int nu_getDecisionConflict( nu_TypeSystem ts, nu_TypeId** expr1, nu_TypeId** expr2);
00321 
00339 int nu_getElementConstructor( nu_TypeSystem ts, int result_handle, unsigned int tps_idx, int iter, uintptr_t* tc, uintptr_t* tcarg);
00340 
00354 int nu_getResultConstructor( nu_TypeSystem ts, int result_handle, uintptr_t* tc, uintptr_t* tcarg);
00355 
00361 void nu_clearResultBuffer( nu_TypeSystem ts);
00362 
00363 #ifdef __cplusplus
00364 }
00365 #endif
00366 #endif
00367 

Generated on Sun Jan 4 15:35:03 2009 for libnaru by  doxygen 1.5.3