예제 #1
0
파일: eval.c 프로젝트: patmanteau/iclc
LONG_DOUBLE eval_expr_binop(eval_context *ctx, ast_node *tree) {
    expr_binop_data *binop_data = (expr_binop_data*)tree->data;
    switch (binop_data->op) {
    case OP_ADD:
        return eval_expr(ctx, binop_data->lhs) + eval_expr(ctx, binop_data->rhs);
    case OP_SUB:
        return eval_expr(ctx, binop_data->lhs) - eval_expr(ctx, binop_data->rhs);
    case OP_MUL:
        return eval_expr(ctx, binop_data->lhs) * eval_expr(ctx, binop_data->rhs);
    case OP_DIV:
        return eval_expr(ctx, binop_data->lhs) / eval_expr(ctx, binop_data->rhs);
    case OP_MOD: {
        // Knuthsche "floored division"
        LONG_DOUBLE a = eval_expr(ctx, binop_data->lhs);
        LONG_DOUBLE n = eval_expr(ctx, binop_data->rhs);
        return a-n*floor(a/n);
    }
    case OP_EXP:
        return pow(eval_expr(ctx, binop_data->lhs), eval_expr(ctx, binop_data->rhs));
    case OP_ASS: {
        if (binop_data->lhs->type != EXPR_VAR) return eval_emit_error(ctx, "Cannot assign to a non-variable.");
        else {
            expr_var_data *var_data = (expr_var_data*)binop_data->lhs->data;
            LONG_DOUBLE rhs_res = eval_expr(ctx, binop_data->rhs);
            store_var(store_get(), var_data->name, rhs_res);
            return rhs_res;
        }
    }
    default: return eval_emit_error(ctx, "Operator not implemented.");
    }
}
예제 #2
0
파일: maker.c 프로젝트: spippolatore/abuse
void list_o_depends(FILE *fp, char *var, char *dir, int platform, int optimize)
{
  char *ch,word[100],tmp[200],o_name[200],sl,*tail;
  if (platform==DOS) sl='\\'; else sl='/';
  get_var(var,z);
  ch=z;
  if (!dir) dir=".";
  dos_path(dir);
  if (optimize) tail="_O"; else tail="_D";
  
  while (fetch_word(&ch,word))
  {
    oextend(dir,word,o_name,platform,optimize);
    if (!get_var(o_name,z))
    { 
      dos_path(word);
      if (platform!=DOS)
      {
	if (!strcmp(dir,"."))
          fprintf(fp,"%s : %s.c\n",o_name,word);
	else
          fprintf(fp,"%s : %s%c%s.c\n",o_name,dir,sl,word);
      }
      else
        fprintf(fp,"%s : .%c%s%c%s.c\n",o_name,sl,dir,sl,word);

      if (platform==DOS)
        fprintf(fp,"\twpp386 %s%c%s.c $(CFLAGS%s) -fo=%s\n",dir,sl,word,tail,o_name);
      else 
        fprintf(fp,"\t$(CC) %s%c%s.c $(CFLAGS%s) -c -o %s\n",dir,sl,word,tail,o_name);


      store_var(o_name,"O");
      if (dir)      
	sprintf(tmp,"%s%c%s.c",dir,sl,word);
      else
      	sprintf(tmp,"%s.c",word);
      list_c_depends(fp,tmp,platform,optimize);
    }
  }

}
예제 #3
0
/* Reverse Polish calculator:
 *
 * Binary operations (+-*\%)
 *     operand operand operator
 *
 * Example: 6 minus 2 in Reverse Polish Notation is "6 2 -"
 */
int main() {
	int type;
	double op2;
	char s[MAXOP];
	char ltr;

	while ((type = getop(s)) != EOF) {
		if (isalpha(type) && islower(type)) {
			push(fetch_var(type));
			continue;
		}
		switch (type) {
			case NUMBER:
				push(atof(s));
				break;
			case '+':
				push(pop() + pop());
				break;
			case '*':
				push(pop() * pop());
				break;
			case '-':
				op2 = pop();
				push(pop() - op2);
				break;
			case '/':
				op2 = pop();
				if (op2 != 0.0) {
					push(pop() / op2);
				} else {
					printf("Error: Cannot divide by zero.\n");
				}
				break;
			/* Yay for modulus! */
			case '%':
				op2 = pop();
				if (op2 != 0.0) {
					push((int)pop() % (int)op2);
				} else {
					printf("Error: Cannot modulo by zero.\n");
				}
				break;
			/* Top of stack */
			case '?':
				stack_top();
				break;
			/* Dupe the top of the stack */
			case '#':
				dupe_top();
				break;
			/* Swap the top two */
			case '~':
				swap_top_two();
				break;
			/* Clear the stack */
			case '!':
				clear_stack();
				break;
			/* sin() support */
			case '(':
				op2 = sin(pop());
				push(op2);
				break;
			/* exp() support */
			case '{':
				op2 = exp(pop());
				push(op2);
				break;
			/* pow() support */
			case '^':
				op2 = pop();
				push(pow(pop(), op2));
				break;
			/* 'lastprint' support */
			case '@':
				push(fetch_last());
				break;
			/* setting variables */
			case '=':
				ltr = getchar();
				if (isalpha(ltr) && islower(ltr)) {
					op2 = pop();
					store_var(ltr, op2);
					push(op2);
				}
				break;
			/* Final output */
			case '\n':
				op2 = pop();
				printf("\t%.8g\n", op2);
				/* Extra Credit: Lets output every non-zero variable! */
				for (ltr = 'a'; ltr <= 'z'; ltr++) {
					if (fetch_var(ltr) != 0) {
						printf("\t%c: %.8g\n", ltr, fetch_var(ltr));
					}
				}
				store_last(op2);
				break;
			default:
				printf("Error: Unknown command %s\n", s);
				break;
		}
	}
	return 0;
}
예제 #4
0
파일: maker.c 프로젝트: spippolatore/abuse
void process_line(char *st, FILE *fp)
{
  char word[100],*wp,*ws,skip,rd;
  expand_line(st,z);
  st=z;

  if (get_word(&st,word))   
  {
    if (!strcmp(word,"INCLUDE"))
    {
      if (!get_word(&st,word))
      {
	fprintf(stderr,"expecting filename after INCLUDE on line %d\n",line_on);
	exit(1);
      } 
      process_file(word);
    } else if (!strcmp(word,"SECTION"))
    {
      skip=0; rd=0;
      wp=st;
      do
      {
	while (*wp==' ') wp++;
	ws=wp;
	if (*wp==0) skip=1;
	else
	{
	  while (*wp!=' ' && *wp) wp++;
	  *wp=0;
	  if (!strcmp(ws,plat_name[detect_platform()]))
	    rd=1;
	}
      } while (!skip && !rd);
      do
      {
	fgets(word,100,fp);
	while (word[strlen(word)-1]=='\n' || word[strlen(word)-1]=='\r')
	  word[strlen(word)-1]=0;
	strcat(word,"\n");
	if (rd)
	{
	  if (strcmp(word,"END\n"))
	  {
	    if (plat_stuff)
	    {
	      plat_stuff=realloc(plat_stuff,strlen(plat_stuff)+1+strlen(word));
	      strcat(plat_stuff,word);
	    }
	    else
	    {
	      plat_stuff=malloc(strlen(plat_stuff)+1+strlen(word));
	      strcpy(plat_stuff,word);
	    }	    
	  }
	}
      } while (strcmp(word,"END\n"));


    } else 
    {
      get_equal(&st);
      store_var(word,st);
    }
  }  
}
예제 #5
0
파일: mimerun.c 프로젝트: LRN/mimerun
/* Runs a handler process specified by full command line <data> (with arguments).
 * Before running:
 * Replaces environment variables.
 * Replaces special MimeRun variables (see above) with the detected values taken from <mres>.
 * Replaces %* and %N with the appropriate bits of <lpfile> and <einfo>->lpParameters.
 * If <executable> is TRUE, waits until the handler process terminates, otherwise returns
 * (almost) immediately.
 */
int run_handler (wchar_t *logfile, wchar_t *data, MimeResults *mres, LPSHELLEXECUTEINFOW einfo, wchar_t *lpfile, wchar_t *lpdir, int executable, int fix_redir)
{
  BOOL ret = 0;
  PVOID redir;
  HMODULE kernel32 = NULL;
  Wow64DisableWow64FsRedirectionFunction disablewow64 = NULL;
  Wow64RevertWow64FsRedirectionFunction revertwow64 = NULL;
  DWORD err;
  int i;
  int depth = 0;
  size_t dupdatalen;
  wchar_t *exp_data = NULL;
  wchar_t *dupdata = NULL;
  wchar_t *dupparams = NULL;
  wchar_t **old_argv = NULL, **newargv = NULL;
  STARTUPINFOW si;
  PROCESS_INFORMATION pi;
  DWORD applelen, typelen, enclen, namelen;
  wchar_t *store_apple = NULL, *store_type = NULL, *store_enc = NULL, *store_name = NULL;

  logtofilew (logfile, L">run_handler\n");

  if (einfo->lpParameters != NULL)
  {
    i = wcslen (einfo->lpParameters) + 1 + wcslen (lpfile);
    logtofilew (logfile, L"Fake commandline length is %d + 1 + %d == %d\n", wcslen (einfo->lpParameters), wcslen (lpfile), i);
    dupparams = (wchar_t *) malloc (sizeof (wchar_t *) * (i + 1));
    if (dupparams == NULL)
    {
      logtofilew (logfile, L"Failed to allocate %d bytes\n", sizeof (wchar_t *) * (i + 1));
      return 1;
    }
    dupparams[0] = L'\0';
    wcscat (dupparams, lpfile);
    wcscat (dupparams, L" ");
    wcscat (dupparams, einfo->lpParameters);
    logtofilew (logfile, L"Fake commandline is %s\n", dupparams);
    if (make_argv (dupparams, &i, NULL) == 0 && i >= 1)
    {
      old_argv = (wchar_t **) malloc (sizeof (wchar_t *) * (i + 1));
      make_argv (dupparams, NULL, old_argv);
      old_argv[i] = NULL;
      logtofilew (logfile, L"Old arguments:\n");
      for (i = 0; old_argv[i] != NULL; i++)
        logtofilew (logfile, L"%2d: %s\n", i, old_argv[i]);
      logtofilew (logfile, L"\n");
    }
    else
    {
      logtofilew (logfile, L"First call to make_argv() have failed or argc <= 0\n");
      free (dupparams);
      return 2;
    }
  }
  else
  {
    old_argv = (wchar_t **) malloc (sizeof (wchar_t *) * 2);
    old_argv[0] = lpfile;
    old_argv[1] = NULL;
    logtofilew (logfile, L"Old arguments:\n");
    for (i = 0; old_argv[i] != NULL; i++)
      logtofilew (logfile, L"%2d: %s\n", i, old_argv[i]);
    logtofilew (logfile, L"\n");
  }

  store_name = store_var (NAME_VARW, &namelen);
  store_type = store_var (TYPE_VARW, &typelen);
  store_enc = store_var (ENC_VARW, &enclen);
  store_apple = store_var (APPLE_VARW, &applelen);

  logtofilew (logfile, L"Backed up variables: {%s}, {%s}, {%s}, {%s}\n", store_name, store_type, store_enc, store_apple);

  SetEnvironmentVariableW (NAME_VARW, mres->wname == NULL ? L"" : mres->wname);
  SetEnvironmentVariableW (TYPE_VARW, mres->wtype == NULL ? L"" : mres->wtype);
  SetEnvironmentVariableW (ENC_VARW, mres->wenc == NULL ? L"" : mres->wenc);
  SetEnvironmentVariableW (APPLE_VARW, mres->wapple == NULL ? L"" : mres->wapple);
  
  exp_data = expand_vars (data, old_argv);

  logtofilew (logfile, L"Commandline with expanded variables: %s\n", exp_data);
  if (dupparams != NULL)
    free (dupparams);
  if (old_argv != NULL)
    free (old_argv);

  SetEnvironmentVariableW (NAME_VARW, store_name);
  SetEnvironmentVariableW (TYPE_VARW, store_type);
  SetEnvironmentVariableW (ENC_VARW, store_enc);
  SetEnvironmentVariableW (APPLE_VARW, store_apple);

  if (store_apple != NULL)
    free (store_apple);
  if (store_type != NULL)
    free (store_type);
  if (store_enc != NULL)
    free (store_enc);
  if (store_name != NULL)
    free (store_name);

  dupdata = (wchar_t *) wcsdup (exp_data);
  if (make_argv (dupdata, &i, NULL) == 0 && i >= 1)
  {
    newargv = (wchar_t **) malloc (sizeof (wchar_t *) * (i + 1));
    make_argv (dupdata, NULL, newargv);
    newargv[i] = NULL;
  }
  else
  {
    logtofilew (logfile, L"First call to make_argv() have failed or argc <= 0\n");
    free (dupdata);
    return 3;
  }

  memset (&si, 0, sizeof (si));
  si.cb = sizeof (si);
  if (einfo->nShow != SW_SHOWDEFAULT)
  {
    si.dwFlags |= STARTF_USESHOWWINDOW;
    si.wShowWindow = einfo->nShow;
    logtofilew (logfile, L"Using nShow == %d\n", si.wShowWindow);
  }


  if (einfo->fMask & SEE_MASK_NO_CONSOLE)
  {
    logtofilew (logfile, L"We will create new console and will not inherit in/out/err handles\n");
  }
  else
  {
    logtofilew (logfile, L"We will not create new console, child process will inherit in/out/err handles\n");
    si.dwFlags |= STARTF_USESTDHANDLES;
    si.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
    si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
    si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
  }

  if (fix_redir && iam32on64 ())
  {
    kernel32 = LoadLibraryW (L"kernel32.dll");
    if (kernel32 != NULL)
    {
      disablewow64 = (Wow64DisableWow64FsRedirectionFunction) GetProcAddress (kernel32, "Wow64DisableWow64FsRedirection");
      revertwow64 = (Wow64RevertWow64FsRedirectionFunction) GetProcAddress (kernel32, "Wow64RevertWow64FsRedirection");
      if (disablewow64 == NULL || revertwow64 == NULL)
        fix_redir = 0;
      else
        fix_redir = disablewow64 (&redir);
    }
    else
      fix_redir = 0;
  }
  else
    fix_redir = 0;

  ret = CreateProcessW (newargv[0], exp_data, NULL, NULL, TRUE, einfo->fMask & SEE_MASK_NO_CONSOLE ? CREATE_NEW_CONSOLE : 0, NULL, lpdir, &si, &pi);
  err = GetLastError();
  if (fix_redir != 0)
    revertwow64 (redir);
  if (kernel32 != NULL)
    FreeLibrary (kernel32);

  if (ret != 0)
  {
    logtofilew (logfile, L"CreateProcessW() succeeded\n");
    ret = 0;
    if (executable)
    {
      logtofilew (logfile, L"Waiting until executable process terminates...\n");
      WaitForSingleObject (pi.hProcess, INFINITE);
      logtofilew (logfile, L"Finished waiting until executable process terminates\n");
    }
    else
    {
      if (einfo->fMask & SEE_MASK_NOCLOSEPROCESS)
      {
        einfo->hProcess = pi.hProcess;
        logtofilew (logfile, L"Will return process handle %08X\n", pi.hProcess);
      }
      if (einfo->fMask & SEE_MASK_WAITFORINPUTIDLE)
      {
        logtofilew (logfile, L"Waiting until non-executable process' input idles...\n");
        WaitForInputIdle (pi.hProcess, 60*1000);
        logtofilew (logfile, L"Finished waiting until non-executable process' input idles\n");
      }
    }
    einfo->hInstApp = (HINSTANCE) 33;
  }
  else
  {
    logtofilew (logfile, L"CreateProcessW() have failed with %d\n", err);
    switch (err)
    {
    case ERROR_FILE_NOT_FOUND:
      einfo->hInstApp = (HINSTANCE) SE_ERR_FNF;
      break;
    case ERROR_PATH_NOT_FOUND:
      einfo->hInstApp = (HINSTANCE) SE_ERR_PNF;
      break;
    case ERROR_ACCESS_DENIED:
      einfo->hInstApp = (HINSTANCE) SE_ERR_ACCESSDENIED;
      break;
    case ERROR_NOT_ENOUGH_MEMORY:
      einfo->hInstApp = (HINSTANCE) SE_ERR_OOM;
      break;
    default:
      einfo->hInstApp = (HINSTANCE) 33;
    }
    ret = 1;
  }
  logtofilew (logfile, L"hInstApp is set to %d\n", einfo->hInstApp);
  free (exp_data);
  if (dupdata != NULL)
    free (dupdata);
  if (newargv != NULL)
    free (newargv);
  logtofilew (logfile, L"<run_handler %d\n", ret);
  return ret;
}
gboolean
gtksnippets_inplaceparser_activate(GtkSnippetsInPlaceParser *self, const gchar* content)
{
	gtksnippets_inplaceparser_deactivate(self);
	
	GtkTextBuffer * buffer = gtk_text_view_get_buffer(self->priv->view);
	
	GtkTextMark *insert = gtk_text_buffer_get_insert(buffer);
	/*TODO remove this gsnippets-parser dependency*/
	if (gsnippets_parser_count_vars(content) <= 0)
	{
		GtkTextIter cur;
		gtk_text_buffer_get_iter_at_mark(buffer,&cur,insert);
		gchar *indent = _compute_line_indentation(self->priv->view,&cur);
		gchar *indent_text = _get_text_with_indent(content, indent);
		g_free(indent);
		gtk_text_buffer_insert_at_cursor(buffer,indent_text,-1);
		g_free(indent_text);
		gtk_text_view_scroll_mark_onscreen(self->priv->view,insert);
		return FALSE;
	}
	
	GtkTextIter start_iter;
	gtk_text_buffer_get_iter_at_mark(buffer,&start_iter,insert);
	GtkTextMark *start_mark = gtk_text_buffer_create_mark(buffer,
							      SNIPPET_START_MARK,
							      &start_iter,
							      TRUE);
	GtkTextMark *end_text_mark = gtk_text_buffer_create_mark(buffer,
							      SNIPPET_END_MARK,
							      &start_iter,
							      FALSE);
	GtkTextMark *end_mark;
	
	gchar *indent = _compute_line_indentation(self->priv->view,&start_iter);
	gchar *indent_text = _get_text_with_indent(content, indent);
	g_free(indent);
	gtk_text_buffer_insert_at_cursor(buffer,indent_text,-1);
	g_free(indent_text);
	gtk_text_view_scroll_mark_onscreen(self->priv->view,end_text_mark);

	gsnippets_func_manager_register_func("cursor",
					     gsnippets_func_empty,
					     self);

	/* Searching variables */
	GtkSnippetsGtvVar *var;
	var = search_var(self,buffer,start_mark,end_text_mark);
	while(var!=NULL)
	{
		end_mark = gtksnippets_gtv_var_get_end_mark(var);
		store_var(self,var);
		var = search_var(self,buffer,end_mark,end_text_mark);
	}
	
	/*Sorting varables by index*/
	self->priv->vars = g_list_sort(self->priv->vars,
					sort_variables);
	
	self->priv->active = TRUE;
	active_next_var(self);
	/*The first var is not handled by cursor-changed. We must set moving to FALSE*/
	self->priv->moving = FALSE;
	g_signal_connect(self->priv->view,"key-press-event",G_CALLBACK(view_key_press_cb),self);
	g_signal_connect_after(buffer,"insert-text",G_CALLBACK(view_insert_text_cb),self);
	g_signal_connect_after(buffer,"delete-range",G_CALLBACK(buffer_delete_range_cb),self);
	g_signal_connect_after(buffer,"mark-set",G_CALLBACK(buffer_mark_set_cb),self);
	g_signal_emit (G_OBJECT (self), signals[PARSER_START], 0);
	
	return TRUE;
}