diff --git a/ase/awk/val.c b/ase/awk/val.c index eedee5a2..41eccf80 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.102 2007-01-02 12:25:18 bacon Exp $ + * $Id: val.c,v 1.103 2007-01-16 14:20:42 bacon Exp $ */ #include @@ -16,12 +16,9 @@ static ase_char_t* __val_real_to_str ( static ase_awk_val_nil_t __awk_nil = { ASE_AWK_VAL_NIL, 0 }; static ase_awk_val_str_t __awk_zls = { ASE_AWK_VAL_STR, 0, ASE_T(""), 0 }; -/* TODO: consider different line ending schemes */ -static ase_awk_val_str_t __awk_nl = { ASE_AWK_VAL_STR, 0, ASE_T("\n"), 1 }; ase_awk_val_t* ase_awk_val_nil = (ase_awk_val_t*)&__awk_nil; ase_awk_val_t* ase_awk_val_zls = (ase_awk_val_t*)&__awk_zls; -ase_awk_val_t* ase_awk_val_nl = (ase_awk_val_t*)&__awk_nl; static ase_awk_val_int_t __awk_int[] = { @@ -266,7 +263,6 @@ ase_bool_t ase_awk_isbuiltinval (ase_awk_val_t* val) return val == ASE_NULL || val == ase_awk_val_nil || val == ase_awk_val_zls || - val == ase_awk_val_nl || val == ase_awk_val_zero || val == ase_awk_val_one || (val >= (ase_awk_val_t*)&__awk_int[0] && diff --git a/ase/awk/val.h b/ase/awk/val.h index f73264e0..854c966b 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.58 2007-01-14 15:07:22 bacon Exp $ + * $Id: val.h,v 1.59 2007-01-16 14:20:42 bacon Exp $ */ #ifndef _ASE_AWK_VAL_H_ @@ -151,7 +151,6 @@ extern "C" { extern ase_awk_val_t* ase_awk_val_nil; extern ase_awk_val_t* ase_awk_val_zls; -extern ase_awk_val_t* ase_awk_val_nl; extern ase_awk_val_t* ase_awk_val_negone; extern ase_awk_val_t* ase_awk_val_zero; extern ase_awk_val_t* ase_awk_val_one; diff --git a/ase/com/Awk.cpp b/ase/com/Awk.cpp index 38037930..d72c7527 100644 --- a/ase/com/Awk.cpp +++ b/ase/com/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp,v 1.18 2007-01-16 06:09:07 bacon Exp $ + * $Id: Awk.cpp,v 1.19 2007-01-16 14:20:42 bacon Exp $ */ #include "stdafx.h" @@ -36,6 +36,7 @@ CAwk::CAwk (): read_src_buf (NULL), write_src_buf (NULL), write_extio_buf (NULL), + bfn_list (NULL), entry_point (NULL), debug (FALSE), use_longlong (FALSE) @@ -63,6 +64,14 @@ CAwk::CAwk (): CAwk::~CAwk () { + while (bfn_list != NULL) + { + bfn_t* next = bfn_list->next; + free (bfn_list->name.ptr); + free (bfn_list); + bfn_list = next; + } + if (entry_point != NULL) SysFreeString (entry_point); if (write_extio_buf != NULL) write_extio_buf->Release (); @@ -464,17 +473,24 @@ HRESULT CAwk::Parse (int* ret) ASE_AWK_DEPTH_REX_MATCH, max_depth.rex.match); } -ase_awk_addbfn (handle, _T("sin"), 3, 0, 1, 1, ASE_NULL, __handle_bfn); -ase_awk_addbfn (handle, _T("cos"), 3, 0, 1, 1, ASE_NULL, __handle_bfn); -ase_awk_addbfn (handle, _T("tan"), 3, 0, 1, 1, ASE_NULL, __handle_bfn); - -/* - for (each buitlin function name... ) + for (bfn_t* bfn = bfn_list; bfn != NULL; bfn = bfn->next) { - ase_awk_addbfn (handle, - ptr, len, 0, min_args, max_args, __handle_bfn); + if (ase_awk_addbfn ( + handle, bfn->name.ptr, bfn->name.len, 0, + bfn->min_args, bfn->max_args, NULL, + __handle_bfn) == NULL) + { + const ase_char_t* msg; + + DBGOUT (_T("cannot add the builtin function")); + + ase_awk_geterror (handle, &errnum, &errlin, &msg); + ase_awk_strxcpy (errmsg, ASE_COUNTOF(errmsg), msg); + + *ret = -1; + return S_OK; + } } -*/ ase_awk_srcios_t srcios; @@ -704,9 +720,46 @@ HRESULT CAwk::Run (int* ret) return S_OK; } -STDMETHODIMP CAwk::AddBuiltinFunction(BSTR name) + +STDMETHODIMP CAwk::AddBuiltinFunction ( + BSTR name, int min_args, int max_args, int* ret) { - /* TODO: remember names */ + bfn_t* bfn = (bfn_t*)malloc (sizeof(bfn_t)); + if (bfn == NULL) + { + errnum = ASE_AWK_ENOMEM; + errlin = 0; + ase_awk_strxcpy ( + errmsg, ASE_COUNTOF(errmsg), + ase_awk_geterrstr(errnum)); + + *ret = -1; + return S_OK; + } + + bfn->name.len = SysStringLen(name); + bfn->name.ptr = (TCHAR*)malloc (bfn->name.len * sizeof(TCHAR)); + if (bfn->name.ptr == NULL) + { + free (bfn); + + errnum = ASE_AWK_ENOMEM; + errlin = 0; + ase_awk_strxcpy ( + errmsg, ASE_COUNTOF(errmsg), + ase_awk_geterrstr(errnum)); + + *ret = -1; + return S_OK; + } + memcpy (bfn->name.ptr, name, sizeof(TCHAR) * bfn->name.len); + + bfn->min_args = min_args; + bfn->max_args = max_args; + bfn->next = bfn_list; + bfn_list = bfn; + + *ret = 0; return S_OK; } diff --git a/ase/com/Awk.h b/ase/com/Awk.h index 47053594..11b5a906 100644 --- a/ase/com/Awk.h +++ b/ase/com/Awk.h @@ -1,5 +1,5 @@ /* - * $Id: Awk.h,v 1.12 2007-01-16 06:09:07 bacon Exp $ + * $Id: Awk.h,v 1.13 2007-01-16 14:20:43 bacon Exp $ */ #ifndef _ASE_COM_AWK_H_ @@ -62,6 +62,18 @@ public: IBuffer* write_extio_buf; + struct bfn_t + { + struct + { + TCHAR* ptr; + size_t len; + } name; + size_t min_args; + size_t max_args; + struct bfn_t* next; + } * bfn_list; + BSTR entry_point; BOOL debug; BOOL use_longlong; @@ -92,7 +104,6 @@ DECLARE_REGISTRY_RESOURCEID(IDR_AWK) // IAwk public: - STDMETHOD(get_UseLongLong)(/*[out, retval]*/ BOOL *pVal); STDMETHOD(put_UseLongLong)(/*[in]*/ BOOL newVal); STDMETHOD(get_Debug)(/*[out, retval]*/ BOOL *pVal); STDMETHOD(put_Debug)(/*[in]*/ BOOL newVal); @@ -139,7 +150,8 @@ public: STDMETHOD(get_ErrorMessage)(/*[out, retval]*/ BSTR *pVal); STDMETHOD(get_ErrorLine)(/*[out, retval]*/ int *pVal); STDMETHOD(get_ErrorCode)(/*[out, retval]*/ int *pVal); - STDMETHOD(AddBuiltinFunction)(BSTR name); + STDMETHOD(AddBuiltinFunction)(/*[in]*/ BSTR name, /*[in]*/ int min_args, /*[in]*/ int max_args, /*[out, retval]*/ int* ret); + STDMETHOD(get_UseLongLong)(/*[out, retval]*/ BOOL *pVal); HRESULT __stdcall Parse (int* ret); HRESULT __stdcall Run (int* ret); }; diff --git a/ase/com/ase.idl b/ase/com/ase.idl index dea08dd8..2d71c73a 100644 --- a/ase/com/ase.idl +++ b/ase/com/ase.idl @@ -1,5 +1,5 @@ /* - * $Id: ase.idl,v 1.13 2007-01-16 06:09:07 bacon Exp $ + * $Id: ase.idl,v 1.14 2007-01-16 14:20:43 bacon Exp $ */ import "oaidl.idl"; @@ -22,7 +22,7 @@ interface IAwk : IDispatch HRESULT Run([out, retval] int* ret); [id(3), helpstring("method AddBuiltinFunction")] - HRESULT AddBuiltinFunction(BSTR name); + HRESULT AddBuiltinFunction([in] BSTR name, [in] int min_args, [in] int max_args, [out, retval] int* ret); [propget, id(4), helpstring("property ErrorCode")] HRESULT ErrorCode([out, retval] int *pVal); diff --git a/ase/test/com/AwkForm.frm b/ase/test/com/AwkForm.frm index 63959383..d58ec2b8 100644 --- a/ase/test/com/AwkForm.frm +++ b/ase/test/com/AwkForm.frm @@ -124,7 +124,7 @@ Begin VB.Form AwkForm Width = 3735 End Begin VB.Label Label2 - Caption = "Deparsed Source Code" + Caption = "Source Out" Height = 255 Left = 5280 TabIndex = 6 @@ -132,7 +132,7 @@ Begin VB.Form AwkForm Width = 3735 End Begin VB.Label Label1 - Caption = "Source Code" + Caption = "Source In" Height = 255 Left = 120 TabIndex = 4 @@ -176,6 +176,14 @@ Private Sub Execute_Click() Awk.Debug = True + If Awk.AddBuiltinFunction("sin", 1, 1) = -1 Then + MsgBox "Cannot add builtin function - " + Awk.ErrorMessage + Exit Sub + End If + Call Awk.AddBuiltinFunction("cos", 1, 1) + Call Awk.AddBuiltinFunction("tan", 1, 1) + Call Awk.AddBuiltinFunction("trim", 1, 1) + If Awk.Parse() = -1 Then MsgBox "PARSE ERROR [" + Str(Awk.ErrorLine) + "]" + Awk.ErrorMessage Else @@ -397,6 +405,8 @@ Function Awk_HandleBuiltinFunction(ByVal name As String, ByVal args As Variant) Awk_HandleBuiltinFunction = Cos(args(0)) ElseIf name = "tan" Then Awk_HandleBuiltinFunction = Tan(args(0)) + ElseIf name = "trim" Then + Awk_HandleBuiltinFunction = Trim(args(0)) End If 'Dim i As Integer